Python-checkins
Threads by month
- ----- 2025 -----
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
November 2013
- 13 participants
- 969 discussions

cpython (2.7): Issue #10734: Fix and re-enable test_ttk test_heading_callback.
by serhiy.storchaka Nov. 2, 2013
by serhiy.storchaka Nov. 2, 2013
Nov. 2, 2013
http://hg.python.org/cpython/rev/0554e2d37bf8
changeset: 86838:0554e2d37bf8
branch: 2.7
parent: 86836:ced345326151
user: Serhiy Storchaka <storchaka(a)gmail.com>
date: Sat Nov 02 10:54:17 2013 +0200
summary:
Issue #10734: Fix and re-enable test_ttk test_heading_callback.
files:
Lib/lib-tk/test/test_ttk/test_widgets.py | 4 +---
1 files changed, 1 insertions(+), 3 deletions(-)
diff --git a/Lib/lib-tk/test/test_ttk/test_widgets.py b/Lib/lib-tk/test/test_ttk/test_widgets.py
--- a/Lib/lib-tk/test/test_ttk/test_widgets.py
+++ b/Lib/lib-tk/test/test_ttk/test_widgets.py
@@ -1370,12 +1370,10 @@
self.assertRaises(Tkinter.TclError, self.tv.heading, '#0',
anchor=1)
- # XXX skipping for now; should be fixed to work with newer ttk
- @unittest.skip("skipping pending resolution of Issue #10734")
def test_heading_callback(self):
def simulate_heading_click(x, y):
support.simulate_mouse_click(self.tv, x, y)
- self.tv.update_idletasks()
+ self.tv.update()
success = [] # no success for now
--
Repository URL: http://hg.python.org/cpython
1
0
http://hg.python.org/cpython/rev/e92bba5b53db
changeset: 86837:e92bba5b53db
parent: 86835:ab7c2c1d349c
parent: 86833:123804a72a8f
user: Serhiy Storchaka <storchaka(a)gmail.com>
date: Sat Nov 02 10:47:57 2013 +0200
summary:
Merge heads
files:
Lib/asyncio/base_events.py | 30 +-
Lib/asyncio/constants.py | 5 +-
Lib/asyncio/events.py | 15 +-
Lib/asyncio/selector_events.py | 154 ++++++---
Lib/asyncio/tasks.py | 5 +-
Lib/asyncio/windows_events.py | 16 +
Lib/asyncio/windows_utils.py | 20 +-
Lib/test/test_asyncio/test_base_events.py | 78 ++++-
Lib/test/test_asyncio/test_events.py | 3 +-
Lib/test/test_asyncio/test_selector_events.py | 154 ++++++---
Lib/test/test_asyncio/test_windows_events.py | 2 +-
11 files changed, 350 insertions(+), 132 deletions(-)
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -186,6 +186,11 @@
self.call_soon(_raise_stop_error)
def close(self):
+ """Close the event loop.
+
+ This clears the queues and shuts down the executor,
+ but does not wait for the executor to finish.
+ """
self._ready.clear()
self._scheduled.clear()
executor = self._default_executor
@@ -275,8 +280,27 @@
@tasks.coroutine
def create_connection(self, protocol_factory, host=None, port=None, *,
ssl=None, family=0, proto=0, flags=0, sock=None,
- local_addr=None):
+ local_addr=None, server_hostname=None):
"""XXX"""
+ if server_hostname is not None and not ssl:
+ raise ValueError('server_hostname is only meaningful with ssl')
+
+ if server_hostname is None and ssl:
+ # Use host as default for server_hostname. It is an error
+ # if host is empty or not set, e.g. when an
+ # already-connected socket was passed or when only a port
+ # is given. To avoid this error, you can pass
+ # server_hostname='' -- this will bypass the hostname
+ # check. (This also means that if host is a numeric
+ # IP/IPv6 address, we will attempt to verify that exact
+ # address; this will probably fail, but it is possible to
+ # create a certificate for a specific IP address, so we
+ # don't judge it here.)
+ if not host:
+ raise ValueError('You must set server_hostname '
+ 'when using ssl without a host')
+ server_hostname = host
+
if host is not None or port is not None:
if sock is not None:
raise ValueError(
@@ -357,7 +381,7 @@
sslcontext = None if isinstance(ssl, bool) else ssl
transport = self._make_ssl_transport(
sock, protocol, sslcontext, waiter,
- server_side=False, server_hostname=host)
+ server_side=False, server_hostname=server_hostname)
else:
transport = self._make_socket_transport(sock, protocol, waiter)
@@ -442,6 +466,8 @@
ssl=None,
reuse_address=None):
"""XXX"""
+ if isinstance(ssl, bool):
+ raise TypeError('ssl argument must be an SSLContext or None')
if host is not None or port is not None:
if sock is not None:
raise ValueError(
diff --git a/Lib/asyncio/constants.py b/Lib/asyncio/constants.py
--- a/Lib/asyncio/constants.py
+++ b/Lib/asyncio/constants.py
@@ -1,4 +1,7 @@
"""Constants."""
+# After the connection is lost, log warnings after this many write()s.
+LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
-LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
+# Seconds to wait before retrying accept().
+ACCEPT_RETRY_DELAY = 1
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -137,6 +137,17 @@
"""Return whether the event loop is currently running."""
raise NotImplementedError
+ def close(self):
+ """Close the loop.
+
+ The loop should not be running.
+
+ This is idempotent and irreversible.
+
+ No other methods should be called after this one.
+ """
+ raise NotImplementedError
+
# Methods scheduling callbacks. All these return Handles.
def call_soon(self, callback, *args):
@@ -172,7 +183,7 @@
def create_connection(self, protocol_factory, host=None, port=None, *,
ssl=None, family=0, proto=0, flags=0, sock=None,
- local_addr=None):
+ local_addr=None, server_hostname=None):
raise NotImplementedError
def create_server(self, protocol_factory, host=None, port=None, *,
@@ -214,6 +225,8 @@
family=0, proto=0, flags=0):
raise NotImplementedError
+ # Pipes and subprocesses.
+
def connect_read_pipe(self, protocol_factory, pipe):
"""Register read pipe in eventloop.
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -5,6 +5,7 @@
"""
import collections
+import errno
import socket
try:
import ssl
@@ -89,28 +90,37 @@
except (BlockingIOError, InterruptedError):
pass
- def _start_serving(self, protocol_factory, sock, ssl=None, server=None):
+ def _start_serving(self, protocol_factory, sock,
+ sslcontext=None, server=None):
self.add_reader(sock.fileno(), self._accept_connection,
- protocol_factory, sock, ssl, server)
+ protocol_factory, sock, sslcontext, server)
- def _accept_connection(self, protocol_factory, sock, ssl=None,
- server=None):
+ def _accept_connection(self, protocol_factory, sock,
+ sslcontext=None, server=None):
try:
conn, addr = sock.accept()
conn.setblocking(False)
- except (BlockingIOError, InterruptedError):
+ except (BlockingIOError, InterruptedError, ConnectionAbortedError):
pass # False alarm.
- except Exception:
- # Bad error. Stop serving.
- self.remove_reader(sock.fileno())
- sock.close()
+ except OSError as exc:
# There's nowhere to send the error, so just log it.
# TODO: Someone will want an error handler for this.
- logger.exception('Accept failed')
+ if exc.errno in (errno.EMFILE, errno.ENFILE,
+ errno.ENOBUFS, errno.ENOMEM):
+ # Some platforms (e.g. Linux keep reporting the FD as
+ # ready, so we remove the read handler temporarily.
+ # We'll try again in a while.
+ logger.exception('Accept out of system resource (%s)', exc)
+ self.remove_reader(sock.fileno())
+ self.call_later(constants.ACCEPT_RETRY_DELAY,
+ self._start_serving,
+ protocol_factory, sock, sslcontext, server)
+ else:
+ raise # The event loop will catch, log and ignore it.
else:
- if ssl:
+ if sslcontext:
self._make_ssl_transport(
- conn, protocol_factory(), ssl, None,
+ conn, protocol_factory(), sslcontext, None,
server_side=True, extra={'peername': addr}, server=server)
else:
self._make_socket_transport(
@@ -277,7 +287,7 @@
err = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
if err != 0:
# Jump to the except clause below.
- raise OSError(err, 'Connect call failed')
+ raise OSError(err, 'Connect call failed %s' % (address,))
except (BlockingIOError, InterruptedError):
self.add_writer(fd, self._sock_connect, fut, True, sock, address)
except Exception as exc:
@@ -404,15 +414,16 @@
try:
self._protocol.pause_writing()
except Exception:
- tulip_log.exception('pause_writing() failed')
+ logger.exception('pause_writing() failed')
def _maybe_resume_protocol(self):
- if self._protocol_paused and self.get_write_buffer_size() <= self._low_water:
+ if (self._protocol_paused and
+ self.get_write_buffer_size() <= self._low_water):
self._protocol_paused = False
try:
self._protocol.resume_writing()
except Exception:
- tulip_log.exception('resume_writing() failed')
+ logger.exception('resume_writing() failed')
def set_write_buffer_limits(self, high=None, low=None):
if high is None:
@@ -548,22 +559,28 @@
def __init__(self, loop, rawsock, protocol, sslcontext, waiter=None,
server_side=False, server_hostname=None,
extra=None, server=None):
+ if ssl is None:
+ raise RuntimeError('stdlib ssl module not available')
+
if server_side:
- assert isinstance(
- sslcontext, ssl.SSLContext), 'Must pass an SSLContext'
+ if not sslcontext:
+ raise ValueError('Server side ssl needs a valid SSLContext')
else:
- # Client-side may pass ssl=True to use a default context.
- # The default is the same as used by urllib.
- if sslcontext is None:
+ if not sslcontext:
+ # Client side may pass ssl=True to use a default
+ # context; in that case the sslcontext passed is None.
+ # The default is the same as used by urllib with
+ # cadefault=True.
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext.options |= ssl.OP_NO_SSLv2
sslcontext.set_default_verify_paths()
sslcontext.verify_mode = ssl.CERT_REQUIRED
+
wrap_kwargs = {
'server_side': server_side,
'do_handshake_on_connect': False,
}
- if server_hostname is not None and not server_side and ssl.HAS_SNI:
+ if server_hostname and not server_side and ssl.HAS_SNI:
wrap_kwargs['server_hostname'] = server_hostname
sslsock = sslcontext.wrap_socket(rawsock, **wrap_kwargs)
@@ -609,7 +626,7 @@
# Verify hostname if requested.
peercert = self._sock.getpeercert()
- if (self._server_hostname is not None and
+ if (self._server_hostname and
self._sslcontext.verify_mode != ssl.CERT_NONE):
try:
ssl.match_hostname(peercert, self._server_hostname)
@@ -625,15 +642,16 @@
compression=self._sock.compression(),
)
- self._loop.add_reader(self._sock_fd, self._on_ready)
- self._loop.add_writer(self._sock_fd, self._on_ready)
+ self._read_wants_write = False
+ self._write_wants_read = False
+ self._loop.add_reader(self._sock_fd, self._read_ready)
self._loop.call_soon(self._protocol.connection_made, self)
if self._waiter is not None:
self._loop.call_soon(self._waiter.set_result, None)
def pause_reading(self):
# XXX This is a bit icky, given the comment at the top of
- # _on_ready(). Is it possible to evoke a deadlock? I don't
+ # _read_ready(). Is it possible to evoke a deadlock? I don't
# know, although it doesn't look like it; write() will still
# accept more data for the buffer and eventually the app will
# call resume_reading() again, and things will flow again.
@@ -648,41 +666,58 @@
self._paused = False
if self._closing:
return
- self._loop.add_reader(self._sock_fd, self._on_ready)
+ self._loop.add_reader(self._sock_fd, self._read_ready)
- def _on_ready(self):
- # Because of renegotiations (?), there's no difference between
- # readable and writable. We just try both. XXX This may be
- # incorrect; we probably need to keep state about what we
- # should do next.
+ def _read_ready(self):
+ if self._write_wants_read:
+ self._write_wants_read = False
+ self._write_ready()
- # First try reading.
- if not self._closing and not self._paused:
- try:
- data = self._sock.recv(self.max_size)
- except (BlockingIOError, InterruptedError,
- ssl.SSLWantReadError, ssl.SSLWantWriteError):
- pass
- except Exception as exc:
- self._fatal_error(exc)
+ if self._buffer:
+ self._loop.add_writer(self._sock_fd, self._write_ready)
+
+ try:
+ data = self._sock.recv(self.max_size)
+ except (BlockingIOError, InterruptedError, ssl.SSLWantReadError):
+ pass
+ except ssl.SSLWantWriteError:
+ self._read_wants_write = True
+ self._loop.remove_reader(self._sock_fd)
+ self._loop.add_writer(self._sock_fd, self._write_ready)
+ except Exception as exc:
+ self._fatal_error(exc)
+ else:
+ if data:
+ self._protocol.data_received(data)
else:
- if data:
- self._protocol.data_received(data)
- else:
- try:
- self._protocol.eof_received()
- finally:
- self.close()
+ try:
+ keep_open = self._protocol.eof_received()
+ if keep_open:
+ logger.warning('returning true from eof_received() '
+ 'has no effect when using ssl')
+ finally:
+ self.close()
- # Now try writing, if there's anything to write.
+ def _write_ready(self):
+ if self._read_wants_write:
+ self._read_wants_write = False
+ self._read_ready()
+
+ if not (self._paused or self._closing):
+ self._loop.add_reader(self._sock_fd, self._read_ready)
+
if self._buffer:
data = b''.join(self._buffer)
self._buffer.clear()
try:
n = self._sock.send(data)
except (BlockingIOError, InterruptedError,
- ssl.SSLWantReadError, ssl.SSLWantWriteError):
+ ssl.SSLWantWriteError):
n = 0
+ except ssl.SSLWantReadError:
+ n = 0
+ self._loop.remove_writer(self._sock_fd)
+ self._write_wants_read = True
except Exception as exc:
self._loop.remove_writer(self._sock_fd)
self._fatal_error(exc)
@@ -691,11 +726,12 @@
if n < len(data):
self._buffer.append(data[n:])
- self._maybe_resume_protocol() # May append to buffer.
+ self._maybe_resume_protocol() # May append to buffer.
- if self._closing and not self._buffer:
+ if not self._buffer:
self._loop.remove_writer(self._sock_fd)
- self._call_connection_lost(None)
+ if self._closing:
+ self._call_connection_lost(None)
def write(self, data):
assert isinstance(data, bytes), repr(type(data))
@@ -708,20 +744,16 @@
self._conn_lost += 1
return
- # We could optimize, but the callback can do this for now.
+ if not self._buffer:
+ self._loop.add_writer(self._sock_fd, self._write_ready)
+
+ # Add it to the buffer.
self._buffer.append(data)
self._maybe_pause_protocol()
def can_write_eof(self):
return False
- def close(self):
- if self._closing:
- return
- self._closing = True
- self._conn_lost += 1
- self._loop.remove_reader(self._sock_fd)
-
class _SelectorDatagramTransport(_SelectorTransport):
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -62,8 +62,9 @@
code = func.__code__
filename = code.co_filename
lineno = code.co_firstlineno
- logger.error('Coroutine %r defined at %s:%s was never yielded from',
- func.__name__, filename, lineno)
+ logger.error(
+ 'Coroutine %r defined at %s:%s was never yielded from',
+ func.__name__, filename, lineno)
def coroutine(func):
diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py
--- a/Lib/asyncio/windows_events.py
+++ b/Lib/asyncio/windows_events.py
@@ -138,6 +138,7 @@
@tasks.coroutine
def start_serving_pipe(self, protocol_factory, address):
server = PipeServer(address)
+
def loop(f=None):
pipe = None
try:
@@ -160,6 +161,7 @@
pipe.close()
else:
f.add_done_callback(loop)
+
self.call_soon(loop)
return [server]
@@ -209,6 +211,7 @@
ov.WSARecv(conn.fileno(), nbytes, flags)
else:
ov.ReadFile(conn.fileno(), nbytes)
+
def finish(trans, key, ov):
try:
return ov.getresult()
@@ -217,6 +220,7 @@
raise ConnectionResetError(*exc.args)
else:
raise
+
return self._register(ov, conn, finish)
def send(self, conn, buf, flags=0):
@@ -226,6 +230,7 @@
ov.WSASend(conn.fileno(), buf, flags)
else:
ov.WriteFile(conn.fileno(), buf)
+
def finish(trans, key, ov):
try:
return ov.getresult()
@@ -234,6 +239,7 @@
raise ConnectionResetError(*exc.args)
else:
raise
+
return self._register(ov, conn, finish)
def accept(self, listener):
@@ -241,6 +247,7 @@
conn = self._get_accept_socket(listener.family)
ov = _overlapped.Overlapped(NULL)
ov.AcceptEx(listener.fileno(), conn.fileno())
+
def finish_accept(trans, key, ov):
ov.getresult()
# Use SO_UPDATE_ACCEPT_CONTEXT so getsockname() etc work.
@@ -249,6 +256,7 @@
_overlapped.SO_UPDATE_ACCEPT_CONTEXT, buf)
conn.settimeout(listener.gettimeout())
return conn, conn.getpeername()
+
return self._register(ov, listener, finish_accept)
def connect(self, conn, address):
@@ -264,26 +272,31 @@
raise
ov = _overlapped.Overlapped(NULL)
ov.ConnectEx(conn.fileno(), address)
+
def finish_connect(trans, key, ov):
ov.getresult()
# Use SO_UPDATE_CONNECT_CONTEXT so getsockname() etc work.
conn.setsockopt(socket.SOL_SOCKET,
_overlapped.SO_UPDATE_CONNECT_CONTEXT, 0)
return conn
+
return self._register(ov, conn, finish_connect)
def accept_pipe(self, pipe):
self._register_with_iocp(pipe)
ov = _overlapped.Overlapped(NULL)
ov.ConnectNamedPipe(pipe.fileno())
+
def finish(trans, key, ov):
ov.getresult()
return pipe
+
return self._register(ov, pipe, finish)
def connect_pipe(self, address):
ov = _overlapped.Overlapped(NULL)
ov.WaitNamedPipeAndConnect(address, self._iocp, ov.address)
+
def finish(err, handle, ov):
# err, handle were arguments passed to PostQueuedCompletionStatus()
# in a function run in a thread pool.
@@ -296,6 +309,7 @@
raise OSError(0, msg, None, err)
else:
return windows_utils.PipeHandle(handle)
+
return self._register(ov, None, finish, wait_for_post=True)
def wait_for_handle(self, handle, timeout=None):
@@ -432,8 +446,10 @@
self._proc = windows_utils.Popen(
args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
bufsize=bufsize, **kwargs)
+
def callback(f):
returncode = self._proc.poll()
self._process_exited(returncode)
+
f = self._loop._proactor.wait_for_handle(int(self._proc._handle))
f.add_done_callback(callback)
diff --git a/Lib/asyncio/windows_utils.py b/Lib/asyncio/windows_utils.py
--- a/Lib/asyncio/windows_utils.py
+++ b/Lib/asyncio/windows_utils.py
@@ -18,18 +18,18 @@
__all__ = ['socketpair', 'pipe', 'Popen', 'PIPE', 'PipeHandle']
-#
+
# Constants/globals
-#
+
BUFSIZE = 8192
PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT
_mmap_counter = itertools.count()
-#
+
# Replacement for socket.socketpair()
-#
+
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
"""A socket pair usable as a self-pipe, for Windows.
@@ -57,9 +57,9 @@
lsock.close()
return (ssock, csock)
-#
+
# Replacement for os.pipe() using handles instead of fds
-#
+
def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
"""Like os.pipe() but with overlapped support and using handles not fds."""
@@ -105,9 +105,9 @@
_winapi.CloseHandle(h2)
raise
-#
+
# Wrapper for a pipe handle
-#
+
class PipeHandle:
"""Wrapper for an overlapped pipe handle which is vaguely file-object like.
@@ -137,9 +137,9 @@
def __exit__(self, t, v, tb):
self.close()
-#
+
# Replacement for subprocess.Popen using overlapped pipe handles
-#
+
class Popen(subprocess.Popen):
"""Replacement for subprocess.Popen using overlapped pipe handles.
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -1,5 +1,6 @@
"""Tests for base_events.py"""
+import errno
import logging
import socket
import time
@@ -8,6 +9,7 @@
from test.support import find_unused_port, IPV6_ENABLED
from asyncio import base_events
+from asyncio import constants
from asyncio import events
from asyncio import futures
from asyncio import protocols
@@ -442,6 +444,71 @@
self.assertRaises(
OSError, self.loop.run_until_complete, coro)
+ def test_create_connection_ssl_server_hostname_default(self):
+ self.loop.getaddrinfo = unittest.mock.Mock()
+
+ def mock_getaddrinfo(*args, **kwds):
+ f = futures.Future(loop=self.loop)
+ f.set_result([(socket.AF_INET, socket.SOCK_STREAM,
+ socket.SOL_TCP, '', ('1.2.3.4', 80))])
+ return f
+
+ self.loop.getaddrinfo.side_effect = mock_getaddrinfo
+ self.loop.sock_connect = unittest.mock.Mock()
+ self.loop.sock_connect.return_value = ()
+ self.loop._make_ssl_transport = unittest.mock.Mock()
+
+ def mock_make_ssl_transport(sock, protocol, sslcontext, waiter,
+ **kwds):
+ waiter.set_result(None)
+
+ self.loop._make_ssl_transport.side_effect = mock_make_ssl_transport
+ ANY = unittest.mock.ANY
+ # First try the default server_hostname.
+ self.loop._make_ssl_transport.reset_mock()
+ coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True)
+ self.loop.run_until_complete(coro)
+ self.loop._make_ssl_transport.assert_called_with(
+ ANY, ANY, ANY, ANY,
+ server_side=False,
+ server_hostname='python.org')
+ # Next try an explicit server_hostname.
+ self.loop._make_ssl_transport.reset_mock()
+ coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True,
+ server_hostname='perl.com')
+ self.loop.run_until_complete(coro)
+ self.loop._make_ssl_transport.assert_called_with(
+ ANY, ANY, ANY, ANY,
+ server_side=False,
+ server_hostname='perl.com')
+ # Finally try an explicit empty server_hostname.
+ self.loop._make_ssl_transport.reset_mock()
+ coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True,
+ server_hostname='')
+ self.loop.run_until_complete(coro)
+ self.loop._make_ssl_transport.assert_called_with(ANY, ANY, ANY, ANY,
+ server_side=False,
+ server_hostname='')
+
+ def test_create_connection_no_ssl_server_hostname_errors(self):
+ # When not using ssl, server_hostname must be None.
+ coro = self.loop.create_connection(MyProto, 'python.org', 80,
+ server_hostname='')
+ self.assertRaises(ValueError, self.loop.run_until_complete, coro)
+ coro = self.loop.create_connection(MyProto, 'python.org', 80,
+ server_hostname='python.org')
+ self.assertRaises(ValueError, self.loop.run_until_complete, coro)
+
+ def test_create_connection_ssl_server_hostname_errors(self):
+ # When using ssl, server_hostname may be None if host is non-empty.
+ coro = self.loop.create_connection(MyProto, '', 80, ssl=True)
+ self.assertRaises(ValueError, self.loop.run_until_complete, coro)
+ coro = self.loop.create_connection(MyProto, None, 80, ssl=True)
+ self.assertRaises(ValueError, self.loop.run_until_complete, coro)
+ coro = self.loop.create_connection(MyProto, None, None,
+ ssl=True, sock=socket.socket())
+ self.assertRaises(ValueError, self.loop.run_until_complete, coro)
+
def test_create_server_empty_host(self):
# if host is empty string use None instead
host = object()
@@ -585,11 +652,18 @@
def test_accept_connection_exception(self, m_log):
sock = unittest.mock.Mock()
sock.fileno.return_value = 10
- sock.accept.side_effect = OSError()
+ sock.accept.side_effect = OSError(errno.EMFILE, 'Too many open files')
+ self.loop.remove_reader = unittest.mock.Mock()
+ self.loop.call_later = unittest.mock.Mock()
self.loop._accept_connection(MyProto, sock)
- self.assertTrue(sock.close.called)
self.assertTrue(m_log.exception.called)
+ self.assertFalse(sock.close.called)
+ self.loop.remove_reader.assert_called_with(10)
+ self.loop.call_later.assert_called_with(constants.ACCEPT_RETRY_DELAY,
+ # self.loop._start_serving
+ unittest.mock.ANY,
+ MyProto, sock, None, None)
if __name__ == '__main__':
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -1276,7 +1276,6 @@
def create_event_loop(self):
return windows_events.SelectorEventLoop()
-
class ProactorEventLoopTests(EventLoopTestsMixin,
SubprocessTestsMixin,
unittest.TestCase):
@@ -1472,6 +1471,8 @@
self.assertRaises(
NotImplementedError, loop.is_running)
self.assertRaises(
+ NotImplementedError, loop.close)
+ self.assertRaises(
NotImplementedError, loop.call_later, None, None)
self.assertRaises(
NotImplementedError, loop.call_at, f, f)
diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py
--- a/Lib/test/test_asyncio/test_selector_events.py
+++ b/Lib/test/test_asyncio/test_selector_events.py
@@ -43,6 +43,7 @@
self.assertIsInstance(
self.loop._make_socket_transport(m, m), _SelectorSocketTransport)
+ @unittest.skipIf(ssl is None, 'No ssl module')
def test_make_ssl_transport(self):
m = unittest.mock.Mock()
self.loop.add_reader = unittest.mock.Mock()
@@ -52,6 +53,16 @@
self.assertIsInstance(
self.loop._make_ssl_transport(m, m, m, m), _SelectorSslTransport)
+ @unittest.mock.patch('asyncio.selector_events.ssl', None)
+ def test_make_ssl_transport_without_ssl_error(self):
+ m = unittest.mock.Mock()
+ self.loop.add_reader = unittest.mock.Mock()
+ self.loop.add_writer = unittest.mock.Mock()
+ self.loop.remove_reader = unittest.mock.Mock()
+ self.loop.remove_writer = unittest.mock.Mock()
+ with self.assertRaises(RuntimeError):
+ self.loop._make_ssl_transport(m, m, m, m)
+
def test_close(self):
ssock = self.loop._ssock
ssock.fileno.return_value = 7
@@ -1003,8 +1014,7 @@
self.loop, self.sock, self.protocol, self.sslcontext,
waiter=waiter)
self.assertTrue(self.sslsock.do_handshake.called)
- self.loop.assert_reader(1, tr._on_ready)
- self.loop.assert_writer(1, tr._on_ready)
+ self.loop.assert_reader(1, tr._read_ready)
test_utils.run_briefly(self.loop)
self.assertIsNone(waiter.result())
@@ -1047,13 +1057,13 @@
def test_pause_resume_reading(self):
tr = self._make_one()
self.assertFalse(tr._paused)
- self.loop.assert_reader(1, tr._on_ready)
+ self.loop.assert_reader(1, tr._read_ready)
tr.pause_reading()
self.assertTrue(tr._paused)
self.assertFalse(1 in self.loop.readers)
tr.resume_reading()
self.assertFalse(tr._paused)
- self.loop.assert_reader(1, tr._on_ready)
+ self.loop.assert_reader(1, tr._read_ready)
def test_write_no_data(self):
transport = self._make_one()
@@ -1084,140 +1094,173 @@
transport.write(b'data')
m_log.warning.assert_called_with('socket.send() raised exception.')
- def test_on_ready_recv(self):
+ def test_read_ready_recv(self):
self.sslsock.recv.return_value = b'data'
transport = self._make_one()
- transport._on_ready()
+ transport._read_ready()
self.assertTrue(self.sslsock.recv.called)
self.assertEqual((b'data',), self.protocol.data_received.call_args[0])
- def test_on_ready_recv_eof(self):
+ def test_read_ready_write_wants_read(self):
+ self.loop.add_writer = unittest.mock.Mock()
+ self.sslsock.recv.side_effect = BlockingIOError
+ transport = self._make_one()
+ transport._write_wants_read = True
+ transport._write_ready = unittest.mock.Mock()
+ transport._buffer.append(b'data')
+ transport._read_ready()
+
+ self.assertFalse(transport._write_wants_read)
+ transport._write_ready.assert_called_with()
+ self.loop.add_writer.assert_called_with(
+ transport._sock_fd, transport._write_ready)
+
+ def test_read_ready_recv_eof(self):
self.sslsock.recv.return_value = b''
transport = self._make_one()
transport.close = unittest.mock.Mock()
- transport._on_ready()
+ transport._read_ready()
transport.close.assert_called_with()
self.protocol.eof_received.assert_called_with()
- def test_on_ready_recv_conn_reset(self):
+ def test_read_ready_recv_conn_reset(self):
err = self.sslsock.recv.side_effect = ConnectionResetError()
transport = self._make_one()
transport._force_close = unittest.mock.Mock()
- transport._on_ready()
+ transport._read_ready()
transport._force_close.assert_called_with(err)
- def test_on_ready_recv_retry(self):
+ def test_read_ready_recv_retry(self):
self.sslsock.recv.side_effect = ssl.SSLWantReadError
transport = self._make_one()
- transport._on_ready()
+ transport._read_ready()
self.assertTrue(self.sslsock.recv.called)
self.assertFalse(self.protocol.data_received.called)
- self.sslsock.recv.side_effect = ssl.SSLWantWriteError
- transport._on_ready()
- self.assertFalse(self.protocol.data_received.called)
-
self.sslsock.recv.side_effect = BlockingIOError
- transport._on_ready()
+ transport._read_ready()
self.assertFalse(self.protocol.data_received.called)
self.sslsock.recv.side_effect = InterruptedError
- transport._on_ready()
+ transport._read_ready()
self.assertFalse(self.protocol.data_received.called)
- def test_on_ready_recv_exc(self):
+ def test_read_ready_recv_write(self):
+ self.loop.remove_reader = unittest.mock.Mock()
+ self.loop.add_writer = unittest.mock.Mock()
+ self.sslsock.recv.side_effect = ssl.SSLWantWriteError
+ transport = self._make_one()
+ transport._read_ready()
+ self.assertFalse(self.protocol.data_received.called)
+ self.assertTrue(transport._read_wants_write)
+
+ self.loop.remove_reader.assert_called_with(transport._sock_fd)
+ self.loop.add_writer.assert_called_with(
+ transport._sock_fd, transport._write_ready)
+
+ def test_read_ready_recv_exc(self):
err = self.sslsock.recv.side_effect = OSError()
transport = self._make_one()
transport._fatal_error = unittest.mock.Mock()
- transport._on_ready()
+ transport._read_ready()
transport._fatal_error.assert_called_with(err)
- def test_on_ready_send(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send(self):
self.sslsock.send.return_value = 4
transport = self._make_one()
transport._buffer = collections.deque([b'data'])
- transport._on_ready()
+ transport._write_ready()
self.assertEqual(collections.deque(), transport._buffer)
self.assertTrue(self.sslsock.send.called)
- def test_on_ready_send_none(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send_none(self):
self.sslsock.send.return_value = 0
transport = self._make_one()
transport._buffer = collections.deque([b'data1', b'data2'])
- transport._on_ready()
+ transport._write_ready()
self.assertTrue(self.sslsock.send.called)
self.assertEqual(collections.deque([b'data1data2']), transport._buffer)
- def test_on_ready_send_partial(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send_partial(self):
self.sslsock.send.return_value = 2
transport = self._make_one()
transport._buffer = collections.deque([b'data1', b'data2'])
- transport._on_ready()
+ transport._write_ready()
self.assertTrue(self.sslsock.send.called)
self.assertEqual(collections.deque([b'ta1data2']), transport._buffer)
- def test_on_ready_send_closing_partial(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send_closing_partial(self):
self.sslsock.send.return_value = 2
transport = self._make_one()
transport._buffer = collections.deque([b'data1', b'data2'])
- transport._on_ready()
+ transport._write_ready()
self.assertTrue(self.sslsock.send.called)
self.assertFalse(self.sslsock.close.called)
- def test_on_ready_send_closing(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send_closing(self):
self.sslsock.send.return_value = 4
transport = self._make_one()
transport.close()
transport._buffer = collections.deque([b'data'])
- transport._on_ready()
+ transport._write_ready()
self.assertFalse(self.loop.writers)
self.protocol.connection_lost.assert_called_with(None)
- def test_on_ready_send_closing_empty_buffer(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send_closing_empty_buffer(self):
self.sslsock.send.return_value = 4
transport = self._make_one()
transport.close()
transport._buffer = collections.deque()
- transport._on_ready()
+ transport._write_ready()
self.assertFalse(self.loop.writers)
self.protocol.connection_lost.assert_called_with(None)
- def test_on_ready_send_retry(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
-
+ def test_write_ready_send_retry(self):
transport = self._make_one()
transport._buffer = collections.deque([b'data'])
- self.sslsock.send.side_effect = ssl.SSLWantReadError
- transport._on_ready()
- self.assertTrue(self.sslsock.send.called)
- self.assertEqual(collections.deque([b'data']), transport._buffer)
-
self.sslsock.send.side_effect = ssl.SSLWantWriteError
- transport._on_ready()
+ transport._write_ready()
self.assertEqual(collections.deque([b'data']), transport._buffer)
self.sslsock.send.side_effect = BlockingIOError()
- transport._on_ready()
+ transport._write_ready()
self.assertEqual(collections.deque([b'data']), transport._buffer)
- def test_on_ready_send_exc(self):
- self.sslsock.recv.side_effect = ssl.SSLWantReadError
+ def test_write_ready_send_read(self):
+ transport = self._make_one()
+ transport._buffer = collections.deque([b'data'])
+
+ self.loop.remove_writer = unittest.mock.Mock()
+ self.sslsock.send.side_effect = ssl.SSLWantReadError
+ transport._write_ready()
+ self.assertFalse(self.protocol.data_received.called)
+ self.assertTrue(transport._write_wants_read)
+ self.loop.remove_writer.assert_called_with(transport._sock_fd)
+
+ def test_write_ready_send_exc(self):
err = self.sslsock.send.side_effect = OSError()
transport = self._make_one()
transport._buffer = collections.deque([b'data'])
transport._fatal_error = unittest.mock.Mock()
- transport._on_ready()
+ transport._write_ready()
transport._fatal_error.assert_called_with(err)
self.assertEqual(collections.deque(), transport._buffer)
+ def test_write_ready_read_wants_write(self):
+ self.loop.add_reader = unittest.mock.Mock()
+ self.sslsock.send.side_effect = BlockingIOError
+ transport = self._make_one()
+ transport._read_wants_write = True
+ transport._read_ready = unittest.mock.Mock()
+ transport._write_ready()
+
+ self.assertFalse(transport._read_wants_write)
+ transport._read_ready.assert_called_with()
+ self.loop.add_reader.assert_called_with(
+ transport._sock_fd, transport._read_ready)
+
def test_write_eof(self):
tr = self._make_one()
self.assertFalse(tr.can_write_eof())
@@ -1245,6 +1288,15 @@
server_hostname='localhost')
+class SelectorSslWithoutSslTransportTests(unittest.TestCase):
+
+ @unittest.mock.patch('asyncio.selector_events.ssl', None)
+ def test_ssl_transport_requires_ssl_module(self):
+ Mock = unittest.mock.Mock
+ with self.assertRaises(RuntimeError):
+ transport = _SelectorSslTransport(Mock(), Mock(), Mock(), Mock())
+
+
class SelectorDatagramTransportTests(unittest.TestCase):
def setUp(self):
diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py
--- a/Lib/test/test_asyncio/test_windows_events.py
+++ b/Lib/test/test_asyncio/test_windows_events.py
@@ -77,7 +77,7 @@
stream_reader = streams.StreamReader(loop=self.loop)
protocol = streams.StreamReaderProtocol(stream_reader)
trans, proto = yield from self.loop.create_pipe_connection(
- lambda:protocol, ADDRESS)
+ lambda: protocol, ADDRESS)
self.assertIsInstance(trans, transports.Transport)
self.assertEqual(protocol, proto)
clients.append((stream_reader, trans))
--
Repository URL: http://hg.python.org/cpython
1
0

cpython (2.7): Issue #19085: Added basic tests for all tkinter widget options.
by serhiy.storchaka Nov. 2, 2013
by serhiy.storchaka Nov. 2, 2013
Nov. 2, 2013
http://hg.python.org/cpython/rev/ced345326151
changeset: 86836:ced345326151
branch: 2.7
parent: 86818:01087a302721
user: Serhiy Storchaka <storchaka(a)gmail.com>
date: Sat Nov 02 10:46:21 2013 +0200
summary:
Issue #19085: Added basic tests for all tkinter widget options.
files:
Lib/lib-tk/test/test_tkinter/test_widgets.py | 916 ++++++++++
Lib/lib-tk/test/test_ttk/support.py | 40 +
Lib/lib-tk/test/test_ttk/test_widgets.py | 488 +++++-
Lib/lib-tk/test/widget_tests.py | 504 +++++
Misc/NEWS | 2 +
5 files changed, 1931 insertions(+), 19 deletions(-)
diff --git a/Lib/lib-tk/test/test_tkinter/test_widgets.py b/Lib/lib-tk/test/test_tkinter/test_widgets.py
new file mode 100644
--- /dev/null
+++ b/Lib/lib-tk/test/test_tkinter/test_widgets.py
@@ -0,0 +1,916 @@
+import unittest
+import Tkinter
+import os
+from test.test_support import requires, run_unittest
+
+from test_ttk.support import tcl_version, requires_tcl, widget_eq
+from widget_tests import (add_standard_options, noconv, int_round,
+ AbstractWidgetTest, StandardOptionsTests,
+ IntegerSizeTests, PixelSizeTests)
+
+requires('gui')
+
+
+class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
+ _conv_pad_pixels = noconv
+
+ def test_class(self):
+ widget = self.create()
+ self.assertEqual(widget['class'],
+ widget.__class__.__name__.title())
+ self.checkInvalidParam(widget, 'class', 'Foo',
+ errmsg="can't modify -class option after widget is created")
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+
+ def test_colormap(self):
+ widget = self.create()
+ self.assertEqual(widget['colormap'], '')
+ self.checkInvalidParam(widget, 'colormap', 'new',
+ errmsg="can't modify -colormap option after widget is created")
+ widget2 = self.create(colormap='new')
+ self.assertEqual(widget2['colormap'], 'new')
+
+ def test_container(self):
+ widget = self.create()
+ self.assertEqual(widget['container'], 0 if self.wantobjects else '0')
+ self.checkInvalidParam(widget, 'container', 1,
+ errmsg="can't modify -container option after widget is created")
+ widget2 = self.create(container=True)
+ self.assertEqual(widget2['container'], 1 if self.wantobjects else '1')
+
+ def test_visual(self):
+ widget = self.create()
+ self.assertEqual(widget['visual'], '')
+ self.checkInvalidParam(widget, 'visual', 'default',
+ errmsg="can't modify -visual option after widget is created")
+ widget2 = self.create(visual='default')
+ self.assertEqual(widget2['visual'], 'default')
+
+
+@add_standard_options(StandardOptionsTests)
+class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'menu', 'padx', 'pady', 'relief', 'screen',
+ 'takefocus', 'use', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Toplevel(self.root, **kwargs)
+
+ def test_menu(self):
+ widget = self.create()
+ menu = Tkinter.Menu(self.root)
+ self.checkParam(widget, 'menu', menu, eq=widget_eq)
+ self.checkParam(widget, 'menu', '')
+
+ def test_screen(self):
+ widget = self.create()
+ self.assertEqual(widget['screen'], '')
+ display = os.environ['DISPLAY']
+ self.checkInvalidParam(widget, 'screen', display,
+ errmsg="can't modify -screen option after widget is created")
+ widget2 = self.create(screen=display)
+ self.assertEqual(widget2['screen'], display)
+
+ def test_use(self):
+ widget = self.create()
+ self.assertEqual(widget['use'], '')
+ widget1 = self.create(container=True)
+ self.assertEqual(widget1['use'], '')
+ self.checkInvalidParam(widget1, 'use', '0x44022',
+ errmsg="can't modify -use option after widget is created")
+ wid = hex(widget1.winfo_id())
+ widget2 = self.create(use=wid)
+ self.assertEqual(widget2['use'], wid)
+
+
+@add_standard_options(StandardOptionsTests)
+class FrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'relief', 'takefocus', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Frame(self.root, **kwargs)
+
+
+@add_standard_options(StandardOptionsTests)
+class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'labelanchor', 'labelwidget', 'padx', 'pady', 'relief',
+ 'takefocus', 'text', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.LabelFrame(self.root, **kwargs)
+
+ def test_labelanchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'labelanchor',
+ 'e', 'en', 'es', 'n', 'ne', 'nw',
+ 's', 'se', 'sw', 'w', 'wn', 'ws')
+ self.checkInvalidParam(widget, 'labelanchor', 'center')
+
+ def test_labelwidget(self):
+ widget = self.create()
+ label = Tkinter.Label(self.root, text='Mupp', name='foo')
+ self.checkParam(widget, 'labelwidget', label, expected='.foo')
+ label.destroy()
+
+
+class AbstractLabelTest(AbstractWidgetTest, IntegerSizeTests):
+ _conv_pixels = noconv
+
+ def test_highlightthickness(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'highlightthickness',
+ 0, 1.3, 2.6, 6, -2, '10p')
+
+
+@add_standard_options(StandardOptionsTests)
+class LabelTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'justify', 'padx', 'pady', 'relief', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Label(self.root, **kwargs)
+
+
+@add_standard_options(StandardOptionsTests)
+class ButtonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor', 'default',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'state', 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength')
+
+ def _create(self, **kwargs):
+ return Tkinter.Button(self.root, **kwargs)
+
+ def test_default(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'default', 'active', 'disabled', 'normal')
+
+
+@add_standard_options(StandardOptionsTests)
+class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify',
+ 'offrelief', 'offvalue', 'onvalue', 'overrelief',
+ 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'tristateimage', 'tristatevalue',
+ 'underline', 'variable', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Checkbutton(self.root, **kwargs)
+
+
+ def test_offvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
+
+ def test_onvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
+
+
+@add_standard_options(StandardOptionsTests)
+class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify', 'offrelief', 'overrelief',
+ 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'tristateimage', 'tristatevalue',
+ 'underline', 'value', 'variable', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Radiobutton(self.root, **kwargs)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
+
+
+@add_standard_options(StandardOptionsTests)
+class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'compound', 'cursor', 'direction',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify', 'menu',
+ 'padx', 'pady', 'relief', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+ _conv_pixels = AbstractWidgetTest._conv_pixels
+
+ def _create(self, **kwargs):
+ return Tkinter.Menubutton(self.root, **kwargs)
+
+ def test_direction(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'direction',
+ 'above', 'below', 'flush', 'left', 'right')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'height', 100, -100, 0, conv=str)
+
+ test_highlightthickness = StandardOptionsTests.test_highlightthickness.im_func
+
+ def test_image(self):
+ widget = self.create()
+ image = Tkinter.PhotoImage('image1')
+ self.checkParam(widget, 'image', image, conv=str)
+ errmsg = 'image "spam" doesn\'t exist'
+ with self.assertRaises(Tkinter.TclError) as cm:
+ widget['image'] = 'spam'
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ with self.assertRaises(Tkinter.TclError) as cm:
+ widget.configure({'image': 'spam'})
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+
+ def test_menu(self):
+ widget = self.create()
+ menu = Tkinter.Menu(widget, name='menu')
+ self.checkParam(widget, 'menu', menu, eq=widget_eq)
+ menu.destroy()
+
+ def test_padx(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, '12m')
+ self.checkParam(widget, 'padx', -2, expected=0)
+
+ def test_pady(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, '12m')
+ self.checkParam(widget, 'pady', -2, expected=0)
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402, -402, 0, conv=str)
+
+
+class OptionMenuTest(MenubuttonTest, unittest.TestCase):
+
+ def _create(self, default='b', values=('a', 'b', 'c'), **kwargs):
+ return Tkinter.OptionMenu(self.root, None, default, *values, **kwargs)
+
+
+@add_standard_options(IntegerSizeTests, StandardOptionsTests)
+class EntryTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth', 'cursor',
+ 'disabledbackground', 'disabledforeground',
+ 'exportselection', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'invalidcommand', 'justify', 'readonlybackground', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'show', 'state', 'takefocus', 'textvariable',
+ 'validate', 'validatecommand', 'width', 'xscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Entry(self.root, **kwargs)
+
+ def test_disabledbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'disabledbackground')
+
+ def test_insertborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertborderwidth', 0, 1.3, -2)
+ self.checkParam(widget, 'insertborderwidth', 2, expected=1)
+ self.checkParam(widget, 'insertborderwidth', '10p', expected=1)
+
+ def test_insertwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertwidth', 1.3, 3.6, '10p')
+ if tcl_version[:2] == (8, 5):
+ self.checkParam(widget, 'insertwidth', 0.9, expected=2)
+ else:
+ self.checkParam(widget, 'insertwidth', 0.9, expected=1)
+ self.checkParam(widget, 'insertwidth', 0.1, expected=2)
+ self.checkParam(widget, 'insertwidth', -2, expected=2)
+
+ def test_invalidcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'invalidcommand')
+ self.checkCommandParam(widget, 'invcmd')
+
+ def test_readonlybackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'readonlybackground')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', '*')
+ self.checkParam(widget, 'show', '')
+ self.checkParam(widget, 'show', ' ')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state',
+ 'disabled', 'normal', 'readonly')
+
+ def test_validate(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'validate',
+ 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
+
+ def test_validatecommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'validatecommand')
+ self.checkCommandParam(widget, 'vcmd')
+
+
+@add_standard_options(StandardOptionsTests)
+class SpinboxTest(EntryTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'background', 'borderwidth',
+ 'buttonbackground', 'buttoncursor', 'buttondownrelief', 'buttonuprelief',
+ 'command', 'cursor', 'disabledbackground', 'disabledforeground',
+ 'exportselection', 'font', 'foreground', 'format', 'from',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'increment',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'invalidcommand', 'justify', 'relief', 'readonlybackground',
+ 'repeatdelay', 'repeatinterval',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'state', 'takefocus', 'textvariable', 'to',
+ 'validate', 'validatecommand', 'values',
+ 'width', 'wrap', 'xscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Spinbox(self.root, **kwargs)
+
+ test_show = None
+
+ def test_buttonbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'buttonbackground')
+
+ def test_buttoncursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'buttoncursor')
+
+ def test_buttondownrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'buttondownrelief')
+
+ def test_buttonuprelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'buttonuprelief')
+
+ def test_format(self):
+ widget = self.create()
+ self.checkParam(widget, 'format', '%2f')
+ self.checkParam(widget, 'format', '%2.2f')
+ self.checkParam(widget, 'format', '%.2f')
+ self.checkParam(widget, 'format', '%2.f')
+ self.checkInvalidParam(widget, 'format', '%2e-1f')
+ self.checkInvalidParam(widget, 'format', '2.2')
+ self.checkInvalidParam(widget, 'format', '%2.-2f')
+ self.checkParam(widget, 'format', '%-2.02f')
+ self.checkParam(widget, 'format', '% 2.02f')
+ self.checkParam(widget, 'format', '% -2.200f')
+ self.checkParam(widget, 'format', '%09.200f')
+ self.checkInvalidParam(widget, 'format', '%d')
+
+ def test_from(self):
+ widget = self.create()
+ self.checkParam(widget, 'to', 100.0)
+ self.checkFloatParam(widget, 'from', -10, 10.2, 11.7)
+ self.checkInvalidParam(widget, 'from', 200,
+ errmsg='-to value must be greater than -from value')
+
+ def test_increment(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'increment', -1, 1, 10.2, 12.8, 0)
+
+ def test_to(self):
+ widget = self.create()
+ self.checkParam(widget, 'from', -100.0)
+ self.checkFloatParam(widget, 'to', -10, 10.2, 11.7)
+ self.checkInvalidParam(widget, 'to', -200,
+ errmsg='-to value must be greater than -from value')
+
+ def test_values(self):
+ # XXX
+ widget = self.create()
+ self.assertEqual(widget['values'], '')
+ self.checkParam(widget, 'values', 'mon tue wed thur')
+ self.checkParam(widget, 'values', ('mon', 'tue', 'wed', 'thur'),
+ expected='mon tue wed thur')
+ self.checkParam(widget, 'values', (42, 3.14, '', 'any string'),
+ expected='42 3.14 {} {any string}')
+ self.checkParam(widget, 'values', '')
+
+ def test_wrap(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'wrap')
+
+
+@add_standard_options(StandardOptionsTests)
+class TextTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'autoseparators', 'background', 'blockcursor', 'borderwidth',
+ 'cursor', 'endline', 'exportselection',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'inactiveselectbackground', 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertunfocussed', 'insertwidth',
+ 'maxundo', 'padx', 'pady', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'setgrid', 'spacing1', 'spacing2', 'spacing3', 'startline', 'state',
+ 'tabs', 'tabstyle', 'takefocus', 'undo', 'width', 'wrap',
+ 'xscrollcommand', 'yscrollcommand',
+ )
+ if tcl_version < (8, 5):
+ wantobjects = False
+
+ def _create(self, **kwargs):
+ return Tkinter.Text(self.root, **kwargs)
+
+ def test_autoseparators(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'autoseparators')
+
+ @requires_tcl(8, 5)
+ def test_blockcursor(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'blockcursor')
+
+ @requires_tcl(8, 5)
+ def test_endline(self):
+ widget = self.create()
+ text = '\n'.join('Line %d' for i in range(100))
+ widget.insert('end', text)
+ self.checkParam(widget, 'endline', 200, expected='')
+ self.checkParam(widget, 'endline', -10, expected='')
+ self.checkInvalidParam(widget, 'endline', 'spam',
+ errmsg='expected integer but got "spam"')
+ self.checkParam(widget, 'endline', 50)
+ self.checkParam(widget, 'startline', 15)
+ self.checkInvalidParam(widget, 'endline', 10,
+ errmsg='-startline must be less than or equal to -endline')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, '3c')
+ self.checkParam(widget, 'height', -100, expected=1)
+ self.checkParam(widget, 'height', 0, expected=1)
+
+ def test_maxundo(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'maxundo', 0, 5, -1)
+
+ @requires_tcl(8, 5)
+ def test_inactiveselectbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'inactiveselectbackground')
+
+ @requires_tcl(8, 6)
+ def test_insertunfocussed(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'insertunfocussed',
+ 'hollow', 'none', 'solid')
+
+ def test_selectborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'selectborderwidth',
+ 1.3, 2.6, -2, '10p', conv=False,
+ keep_orig=tcl_version >= (8, 5))
+
+ def test_spacing1(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing1', 20, 21.4, 22.6, '0.5c')
+ self.checkParam(widget, 'spacing1', -5, expected=0)
+
+ def test_spacing2(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing2', 5, 6.4, 7.6, '0.1c')
+ self.checkParam(widget, 'spacing2', -1, expected=0)
+
+ def test_spacing3(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing3', 20, 21.4, 22.6, '0.5c')
+ self.checkParam(widget, 'spacing3', -10, expected=0)
+
+ @requires_tcl(8, 5)
+ def test_startline(self):
+ widget = self.create()
+ text = '\n'.join('Line %d' for i in range(100))
+ widget.insert('end', text)
+ self.checkParam(widget, 'startline', 200, expected='')
+ self.checkParam(widget, 'startline', -10, expected='')
+ self.checkInvalidParam(widget, 'startline', 'spam',
+ errmsg='expected integer but got "spam"')
+ self.checkParam(widget, 'startline', 10)
+ self.checkParam(widget, 'endline', 50)
+ self.checkInvalidParam(widget, 'startline', 70,
+ errmsg='-startline must be less than or equal to -endline')
+
+ def test_state(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkParams(widget, 'state', 'disabled', 'normal')
+ else:
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal')
+
+ def test_tabs(self):
+ widget = self.create()
+ self.checkParam(widget, 'tabs', (10.2, 20.7, '1i', '2i'))
+ self.checkParam(widget, 'tabs', '10.2 20.7 1i 2i',
+ expected=('10.2', '20.7', '1i', '2i'))
+ self.checkParam(widget, 'tabs', '2c left 4c 6c center',
+ expected=('2c', 'left', '4c', '6c', 'center'))
+ self.checkInvalidParam(widget, 'tabs', 'spam',
+ errmsg='bad screen distance "spam"',
+ keep_orig=tcl_version >= (8, 5))
+
+ @requires_tcl(8, 5)
+ def test_tabstyle(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'tabstyle', 'tabular', 'wordprocessor')
+
+ def test_undo(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'undo')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402)
+ self.checkParam(widget, 'width', -402, expected=1)
+ self.checkParam(widget, 'width', 0, expected=1)
+
+ def test_wrap(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkParams(widget, 'wrap', 'char', 'none', 'word')
+ else:
+ self.checkEnumParam(widget, 'wrap', 'char', 'none', 'word')
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class CanvasTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'closeenough', 'confine', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'relief', 'scrollregion',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'state', 'takefocus',
+ 'xscrollcommand', 'xscrollincrement',
+ 'yscrollcommand', 'yscrollincrement', 'width',
+ )
+
+ _conv_pixels = staticmethod(int_round)
+ wantobjects = False
+
+ def _create(self, **kwargs):
+ return Tkinter.Canvas(self.root, **kwargs)
+
+ def test_closeenough(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'closeenough', 24, 2.4, 3.6, -3,
+ conv=float)
+
+ def test_confine(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'confine')
+
+ def test_scrollregion(self):
+ widget = self.create()
+ self.checkParam(widget, 'scrollregion', '0 0 200 150')
+ self.checkParam(widget, 'scrollregion', (0, 0, 200, 150),
+ expected='0 0 200 150')
+ self.checkParam(widget, 'scrollregion', '')
+ self.checkInvalidParam(widget, 'scrollregion', 'spam',
+ errmsg='bad scrollRegion "spam"')
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 'spam'))
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200))
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 150, 0))
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal',
+ errmsg='bad state value "{}": must be normal or disabled')
+
+ def test_xscrollincrement(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'xscrollincrement',
+ 40, 0, 41.2, 43.6, -40, '0.5i')
+
+ def test_yscrollincrement(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'yscrollincrement',
+ 10, 0, 11.2, 13.6, -10, '0.1i')
+
+
+@add_standard_options(IntegerSizeTests, StandardOptionsTests)
+class ListboxTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activestyle', 'background', 'borderwidth', 'cursor',
+ 'disabledforeground', 'exportselection',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'listvariable', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'selectmode', 'setgrid', 'state',
+ 'takefocus', 'width', 'xscrollcommand', 'yscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return Tkinter.Listbox(self.root, **kwargs)
+
+ def test_activestyle(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'activestyle',
+ 'dotbox', 'none', 'underline')
+
+ def test_listvariable(self):
+ widget = self.create()
+ var = Tkinter.DoubleVar()
+ self.checkVariableParam(widget, 'listvariable', var)
+
+ def test_selectmode(self):
+ widget = self.create()
+ self.checkParam(widget, 'selectmode', 'single')
+ self.checkParam(widget, 'selectmode', 'browse')
+ self.checkParam(widget, 'selectmode', 'multiple')
+ self.checkParam(widget, 'selectmode', 'extended')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal')
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class ScaleTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'background', 'bigincrement', 'borderwidth',
+ 'command', 'cursor', 'digits', 'font', 'foreground', 'from',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'label', 'length', 'orient', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'resolution', 'showvalue', 'sliderlength', 'sliderrelief', 'state',
+ 'takefocus', 'tickinterval', 'to', 'troughcolor', 'variable', 'width',
+ )
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return Tkinter.Scale(self.root, **kwargs)
+
+ def test_bigincrement(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'bigincrement', 12.4, 23.6, -5)
+
+ def test_digits(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'digits', 5, 0)
+
+ def test_from(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=round)
+
+ def test_label(self):
+ widget = self.create()
+ self.checkParam(widget, 'label', 'any string')
+ self.checkParam(widget, 'label', '')
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
+
+ def test_resolution(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'resolution', 4.2, 0, 6.7, -2)
+
+ def test_showvalue(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'showvalue')
+
+ def test_sliderlength(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sliderlength',
+ 10, 11.2, 15.6, -3, '3m')
+
+ def test_sliderrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'sliderrelief')
+
+ def test_tickinterval(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'tickinterval', 1, 4.3, 7.6, 0,
+ conv=round)
+ self.checkParam(widget, 'tickinterval', -2, expected=2,
+ conv=round)
+
+ def test_to(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10,
+ conv=round)
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activerelief',
+ 'background', 'borderwidth',
+ 'command', 'cursor', 'elementborderwidth',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'jump', 'orient', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'takefocus', 'troughcolor', 'width',
+ )
+ _conv_pixels = staticmethod(int_round)
+ wantobjects = False
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return Tkinter.Scrollbar(self.root, **kwargs)
+
+ def test_activerelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'activerelief')
+
+ def test_elementborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'elementborderwidth', 4.3, 5.6, -2, '1m')
+
+ def test_orient(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'orient', 'vertical', 'horizontal',
+ errmsg='bad orientation "{}": must be vertical or horizontal')
+
+
+@add_standard_options(StandardOptionsTests)
+class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth', 'cursor',
+ 'handlepad', 'handlesize', 'height',
+ 'opaqueresize', 'orient', 'relief',
+ 'sashcursor', 'sashpad', 'sashrelief', 'sashwidth',
+ 'showhandle', 'width',
+ )
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return Tkinter.PanedWindow(self.root, **kwargs)
+
+ def test_handlepad(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'handlepad', 5, 6.4, 7.6, -3, '1m')
+
+ def test_handlesize(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'handlesize', 8, 9.4, 10.6, -3, '2m',
+ conv=noconv)
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i',
+ conv=noconv)
+
+ def test_opaqueresize(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'opaqueresize')
+
+ def test_sashcursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'sashcursor')
+
+ def test_sashpad(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sashpad', 8, 1.3, 2.6, -2, '2m')
+
+ def test_sashrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'sashrelief')
+
+ def test_sashwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sashwidth', 10, 11.1, 15.6, -3, '1m',
+ conv=noconv)
+
+ def test_showhandle(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'showhandle')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i',
+ conv=noconv)
+
+
+@add_standard_options(StandardOptionsTests)
+class MenuTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeborderwidth', 'activeforeground',
+ 'background', 'borderwidth', 'cursor',
+ 'disabledforeground', 'font', 'foreground',
+ 'postcommand', 'relief', 'selectcolor', 'takefocus',
+ 'tearoff', 'tearoffcommand', 'title', 'type',
+ )
+ _conv_pixels = noconv
+
+ def _create(self, **kwargs):
+ return Tkinter.Menu(self.root, **kwargs)
+
+ def test_postcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'postcommand')
+
+ def test_tearoff(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'tearoff')
+
+ def test_tearoffcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'tearoffcommand')
+
+ def test_title(self):
+ widget = self.create()
+ self.checkParam(widget, 'title', 'any string')
+
+ def test_type(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'type',
+ 'normal', 'tearoff', 'menubar')
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class MessageTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'anchor', 'aspect', 'background', 'borderwidth',
+ 'cursor', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'justify', 'padx', 'pady', 'relief',
+ 'takefocus', 'text', 'textvariable', 'width',
+ )
+ _conv_pad_pixels = noconv
+
+ def _create(self, **kwargs):
+ return Tkinter.Message(self.root, **kwargs)
+
+ def test_aspect(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'aspect', 250, 0, -300)
+
+
+tests_gui = [
+ ButtonTest, CanvasTest, CheckbuttonTest, EntryTest,
+ FrameTest, LabelFrameTest,LabelTest, ListboxTest,
+ MenubuttonTest, MenuTest, MessageTest, OptionMenuTest,
+ PanedWindowTest, RadiobuttonTest, ScaleTest, ScrollbarTest,
+ SpinboxTest, TextTest, ToplevelTest,
+]
+
+if __name__ == '__main__':
+ run_unittest(*tests_gui)
diff --git a/Lib/lib-tk/test/test_ttk/support.py b/Lib/lib-tk/test/test_ttk/support.py
--- a/Lib/lib-tk/test/test_ttk/support.py
+++ b/Lib/lib-tk/test/test_ttk/support.py
@@ -1,3 +1,4 @@
+import unittest
import Tkinter
def get_tk_root():
@@ -31,3 +32,42 @@
widget.event_generate('<Motion>', x=x, y=y)
widget.event_generate('<ButtonPress-1>', x=x, y=y)
widget.event_generate('<ButtonRelease-1>', x=x, y=y)
+
+
+import _tkinter
+tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.')))
+
+def requires_tcl(*version):
+ return unittest.skipUnless(tcl_version >= version,
+ 'requires Tcl version >= ' + '.'.join(map(str, version)))
+
+units = {
+ 'c': 72 / 2.54, # centimeters
+ 'i': 72, # inches
+ 'm': 72 / 25.4, # millimeters
+ 'p': 1, # points
+}
+
+def pixels_conv(value):
+ return float(value[:-1]) * units[value[-1:]]
+
+def tcl_obj_eq(actual, expected):
+ if actual == expected:
+ return True
+ if isinstance(actual, _tkinter.Tcl_Obj):
+ if isinstance(expected, str):
+ return str(actual) == expected
+ if isinstance(actual, tuple):
+ if isinstance(expected, tuple):
+ return (len(actual) == len(expected) and
+ all(tcl_obj_eq(act, exp)
+ for act, exp in zip(actual, expected)))
+ return False
+
+def widget_eq(actual, expected):
+ if actual == expected:
+ return True
+ if isinstance(actual, (str, Tkinter.Widget)):
+ if isinstance(expected, (str, Tkinter.Widget)):
+ return str(actual) == str(expected)
+ return False
diff --git a/Lib/lib-tk/test/test_ttk/test_widgets.py b/Lib/lib-tk/test/test_ttk/test_widgets.py
--- a/Lib/lib-tk/test/test_ttk/test_widgets.py
+++ b/Lib/lib-tk/test/test_ttk/test_widgets.py
@@ -6,9 +6,53 @@
import support
from test_functions import MockTclObj, MockStateSpec
+from support import tcl_version
+from widget_tests import (add_standard_options, noconv,
+ AbstractWidgetTest, StandardOptionsTests,
+ IntegerSizeTests, PixelSizeTests)
requires('gui')
+
+class StandardTtkOptionsTests(StandardOptionsTests):
+
+ def test_class(self):
+ widget = self.create()
+ self.assertEqual(widget['class'], '')
+ errmsg='attempt to change read-only option'
+ if tcl_version < (8, 6):
+ errmsg='Attempt to change read-only option'
+ self.checkInvalidParam(widget, 'class', 'Foo', errmsg=errmsg)
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+
+ def test_padding(self):
+ widget = self.create()
+ self.checkParam(widget, 'padding', 0, expected=('0',))
+ self.checkParam(widget, 'padding', 5, expected=('5',))
+ self.checkParam(widget, 'padding', (5, 6), expected=('5', '6'))
+ self.checkParam(widget, 'padding', (5, 6, 7),
+ expected=('5', '6', '7'))
+ self.checkParam(widget, 'padding', (5, 6, 7, 8),
+ expected=('5', '6', '7', '8'))
+ self.checkParam(widget, 'padding', ('5p', '6p', '7p', '8p'))
+ self.checkParam(widget, 'padding', (), expected='')
+
+ def test_style(self):
+ widget = self.create()
+ self.assertEqual(widget['style'], '')
+ errmsg = 'Layout Foo not found'
+ if hasattr(self, 'default_orient'):
+ errmsg = ('Layout %s.Foo not found' %
+ getattr(self, 'default_orient').title())
+ self.checkInvalidParam(widget, 'style', 'Foo',
+ errmsg=errmsg)
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+ # XXX
+ pass
+
+
class WidgetTest(unittest.TestCase):
"""Tests methods available in every ttk widget."""
@@ -72,7 +116,112 @@
self.assertEqual(self.widget.state(), ('active', ))
-class ButtonTest(unittest.TestCase):
+class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
+ _conv_pixels = noconv
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class FrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'borderwidth', 'class', 'cursor', 'height',
+ 'padding', 'relief', 'style', 'takefocus',
+ 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Frame(self.root, **kwargs)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'borderwidth', 'class', 'cursor', 'height',
+ 'labelanchor', 'labelwidget',
+ 'padding', 'relief', 'style', 'takefocus',
+ 'text', 'underline', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.LabelFrame(self.root, **kwargs)
+
+ def test_labelanchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'labelanchor',
+ 'e', 'en', 'es', 'n', 'ne', 'nw', 's', 'se', 'sw', 'w', 'wn', 'ws',
+ errmsg='Bad label anchor specification {}')
+ self.checkInvalidParam(widget, 'labelanchor', 'center')
+
+ def test_labelwidget(self):
+ widget = self.create()
+ label = ttk.Label(self.root, text='Mupp', name='foo')
+ self.checkParam(widget, 'labelwidget', label, expected='.foo')
+ label.destroy()
+
+
+class AbstractLabelTest(AbstractWidgetTest):
+
+ def checkImageParam(self, widget, name):
+ image = Tkinter.PhotoImage('image1')
+ image2 = Tkinter.PhotoImage('image2')
+ self.checkParam(widget, name, image, expected=('image1',))
+ self.checkParam(widget, name, 'image1', expected=('image1',))
+ self.checkParam(widget, name, (image,), expected=('image1',))
+ self.checkParam(widget, name, (image, 'active', image2),
+ expected=('image1', 'active', 'image2'))
+ self.checkParam(widget, name, 'image1 active image2',
+ expected=('image1', 'active', 'image2'))
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='image "spam" doesn\'t exist')
+
+ def test_compound(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'compound',
+ 'none', 'text', 'image', 'center',
+ 'top', 'bottom', 'left', 'right')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkParams(widget, 'width', 402, -402, 0)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class LabelTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'anchor', 'background',
+ 'class', 'compound', 'cursor', 'font', 'foreground',
+ 'image', 'justify', 'padding', 'relief', 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+ _conv_pixels = noconv
+
+ def _create(self, **kwargs):
+ return ttk.Label(self.root, **kwargs)
+
+ def test_font(self):
+ widget = self.create()
+ self.checkParam(widget, 'font',
+ '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class ButtonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor', 'default',
+ 'image', 'state', 'style', 'takefocus', 'text', 'textvariable',
+ 'underline', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Button(self.root, **kwargs)
+
+ def test_default(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'default', 'normal', 'active', 'disabled')
def test_invoke(self):
success = []
@@ -81,7 +230,27 @@
self.assertTrue(success)
-class CheckbuttonTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor',
+ 'image',
+ 'offvalue', 'onvalue',
+ 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'variable', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Checkbutton(self.root, **kwargs)
+
+ def test_offvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
+
+ def test_onvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
def test_invoke(self):
success = []
@@ -104,21 +273,40 @@
cbtn['command'] = ''
res = cbtn.invoke()
- self.assertEqual(str(res), '')
+ self.assertFalse(str(res))
self.assertFalse(len(success) > 1)
self.assertEqual(cbtn['offvalue'],
cbtn.tk.globalgetvar(cbtn['variable']))
-class ComboboxTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class ComboboxTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'exportselection', 'height',
+ 'justify', 'postcommand', 'state', 'style',
+ 'takefocus', 'textvariable', 'values', 'width',
+ )
def setUp(self):
+ super(ComboboxTest, self).setUp()
support.root_deiconify()
- self.combo = ttk.Combobox()
+ self.combo = self.create()
def tearDown(self):
self.combo.destroy()
support.root_withdraw()
+ super(ComboboxTest, self).tearDown()
+
+ def _create(self, **kwargs):
+ return ttk.Combobox(self.root, **kwargs)
+
+ def test_height(self):
+ widget = self.create()
+ self.checkParams(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
def _show_drop_down_listbox(self):
width = self.combo.winfo_width()
@@ -166,8 +354,16 @@
self.assertEqual(self.combo.get(), getval)
self.assertEqual(self.combo.current(), currval)
+ self.assertEqual(self.combo['values'],
+ () if tcl_version < (8, 5) else '')
check_get_current('', -1)
+ self.checkParam(self.combo, 'values', 'mon tue wed thur',
+ expected=('mon', 'tue', 'wed', 'thur'))
+ self.checkParam(self.combo, 'values', ('mon', 'tue', 'wed', 'thur'))
+ self.checkParam(self.combo, 'values', (42, 3.14, '', 'any string'))
+ self.checkParam(self.combo, 'values', '')
+
self.combo['values'] = ['a', 1, 'c']
self.combo.set('c')
@@ -208,15 +404,52 @@
combo2.destroy()
-class EntryTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class EntryTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'class', 'cursor',
+ 'exportselection', 'font',
+ 'invalidcommand', 'justify',
+ 'show', 'state', 'style', 'takefocus', 'textvariable',
+ 'validate', 'validatecommand', 'width', 'xscrollcommand',
+ )
def setUp(self):
+ super(EntryTest, self).setUp()
support.root_deiconify()
- self.entry = ttk.Entry()
+ self.entry = self.create()
def tearDown(self):
self.entry.destroy()
support.root_withdraw()
+ super(EntryTest, self).tearDown()
+
+ def _create(self, **kwargs):
+ return ttk.Entry(self.root, **kwargs)
+
+ def test_invalidcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'invalidcommand')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', '*')
+ self.checkParam(widget, 'show', '')
+ self.checkParam(widget, 'show', ' ')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state',
+ 'disabled', 'normal', 'readonly')
+
+ def test_validate(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'validate',
+ 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
+
+ def test_validatecommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'validatecommand')
def test_bbox(self):
@@ -312,16 +545,36 @@
self.assertEqual(self.entry.state(), ())
-class PanedwindowTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'height',
+ 'orient', 'style', 'takefocus', 'width',
+ )
def setUp(self):
+ super(PanedWindowTest, self).setUp()
support.root_deiconify()
- self.paned = ttk.Panedwindow()
+ self.paned = self.create()
def tearDown(self):
self.paned.destroy()
support.root_withdraw()
+ super(PanedWindowTest, self).tearDown()
+ def _create(self, **kwargs):
+ return ttk.PanedWindow(self.root, **kwargs)
+
+ def test_orient(self):
+ widget = self.create()
+ self.assertEqual(str(widget['orient']), 'vertical')
+ errmsg='attempt to change read-only option'
+ if tcl_version < (8, 6):
+ errmsg='Attempt to change read-only option'
+ self.checkInvalidParam(widget, 'orient', 'horizontal',
+ errmsg=errmsg)
+ widget2 = self.create(orient='horizontal')
+ self.assertEqual(str(widget2['orient']), 'horizontal')
def test_add(self):
# attempt to add a child that is not a direct child of the paned window
@@ -431,7 +684,22 @@
self.assertTrue(isinstance(self.paned.sashpos(0), int))
-class RadiobuttonTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor',
+ 'image',
+ 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'value', 'variable', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Radiobutton(self.root, **kwargs)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
def test_invoke(self):
success = []
@@ -461,19 +729,68 @@
self.assertEqual(str(cbtn['variable']), str(cbtn2['variable']))
+class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'compound', 'cursor', 'direction',
+ 'image', 'menu', 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width',
+ )
-class ScaleTest(unittest.TestCase):
+ def _create(self, **kwargs):
+ return ttk.Menubutton(self.root, **kwargs)
+
+ def test_direction(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'direction',
+ 'above', 'below', 'left', 'right', 'flush')
+
+ def test_menu(self):
+ widget = self.create()
+ menu = Tkinter.Menu(widget, name='menu')
+ self.checkParam(widget, 'menu', menu, conv=str)
+ menu.destroy()
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class ScaleTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'cursor', 'from', 'length',
+ 'orient', 'style', 'takefocus', 'to', 'value', 'variable',
+ )
+ _conv_pixels = noconv
+ default_orient = 'horizontal'
def setUp(self):
+ super(ScaleTest, self).setUp()
support.root_deiconify()
- self.scale = ttk.Scale()
+ self.scale = self.create()
self.scale.pack()
self.scale.update()
def tearDown(self):
self.scale.destroy()
support.root_withdraw()
+ super(ScaleTest, self).tearDown()
+ def _create(self, **kwargs):
+ return ttk.Scale(self.root, **kwargs)
+
+ def test_from(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=False)
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
+
+ def test_to(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10, conv=False)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'value', 300, 14.9, 15.1, -10, conv=False)
def test_custom_event(self):
failure = [1, 1, 1] # will need to be empty
@@ -538,11 +855,64 @@
self.assertRaises(Tkinter.TclError, self.scale.set, None)
-class NotebookTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class ProgressbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'orient', 'length',
+ 'mode', 'maximum', 'phase',
+ 'style', 'takefocus', 'value', 'variable',
+ )
+ _conv_pixels = noconv
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return ttk.Progressbar(self.root, **kwargs)
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 100.1, 56.7, '2i')
+
+ def test_maximum(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'maximum', 150.2, 77.7, 0, -10, conv=False)
+
+ def test_mode(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'mode', 'determinate', 'indeterminate')
+
+ def test_phase(self):
+ # XXX
+ pass
+
+ def test_value(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'value', 150.2, 77.7, 0, -10,
+ conv=False)
+
+
+(a)unittest.skipIf(sys.platform == 'darwin',
+ 'ttk.Scrollbar is special on MacOSX')
+@add_standard_options(StandardTtkOptionsTests)
+class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'cursor', 'orient', 'style', 'takefocus',
+ )
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return ttk.Scrollbar(self.root, **kwargs)
+
+
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class NotebookTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'height', 'padding', 'style', 'takefocus',
+ )
def setUp(self):
+ super(NotebookTest, self).setUp()
support.root_deiconify()
- self.nb = ttk.Notebook(padding=0)
+ self.nb = self.create(padding=0)
self.child1 = ttk.Label()
self.child2 = ttk.Label()
self.nb.add(self.child1, text='a')
@@ -553,7 +923,10 @@
self.child2.destroy()
self.nb.destroy()
support.root_withdraw()
+ super(NotebookTest, self).tearDown()
+ def _create(self, **kwargs):
+ return ttk.Notebook(self.root, **kwargs)
def test_tab_identifiers(self):
self.nb.forget(0)
@@ -745,16 +1118,68 @@
self.assertEqual(self.nb.select(), str(self.child1))
-class TreeviewTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'columns', 'cursor', 'displaycolumns',
+ 'height', 'padding', 'selectmode', 'show',
+ 'style', 'takefocus', 'xscrollcommand', 'yscrollcommand',
+ )
def setUp(self):
+ super(TreeviewTest, self).setUp()
support.root_deiconify()
- self.tv = ttk.Treeview(padding=0)
+ self.tv = self.create(padding=0)
def tearDown(self):
self.tv.destroy()
support.root_withdraw()
+ super(TreeviewTest, self).tearDown()
+ def _create(self, **kwargs):
+ return ttk.Treeview(self.root, **kwargs)
+
+ def test_columns(self):
+ widget = self.create()
+ self.checkParam(widget, 'columns', 'a b c',
+ expected=('a', 'b', 'c'))
+ self.checkParam(widget, 'columns', ('a', 'b', 'c'))
+ self.checkParam(widget, 'columns', '')
+
+ def test_displaycolumns(self):
+ widget = self.create()
+ widget['columns'] = ('a', 'b', 'c')
+ self.checkParam(widget, 'displaycolumns', 'b a c',
+ expected=('b', 'a', 'c'))
+ self.checkParam(widget, 'displaycolumns', ('b', 'a', 'c'))
+ self.checkParam(widget, 'displaycolumns', '#all',
+ expected=('#all',))
+ self.checkParam(widget, 'displaycolumns', (2, 1, 0))
+ self.checkInvalidParam(widget, 'displaycolumns', ('a', 'b', 'd'),
+ errmsg='Invalid column index d')
+ self.checkInvalidParam(widget, 'displaycolumns', (1, 2, 3),
+ errmsg='Column index 3 out of bounds')
+ self.checkInvalidParam(widget, 'displaycolumns', (1, -2),
+ errmsg='Column index -2 out of bounds')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, -100, 0, '3c', conv=False)
+ self.checkPixelsParam(widget, 'height', 101.2, 102.6, conv=noconv)
+
+ def test_selectmode(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'selectmode',
+ 'none', 'browse', 'extended')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', 'tree headings',
+ expected=('tree', 'headings'))
+ self.checkParam(widget, 'show', ('tree', 'headings'))
+ self.checkParam(widget, 'show', ('headings', 'tree'))
+ self.checkParam(widget, 'show', 'tree', expected=('tree',))
+ self.checkParam(widget, 'show', 'headings', expected=('headings',))
def test_bbox(self):
self.tv.pack()
@@ -1148,10 +1573,35 @@
self.assertTrue(isinstance(self.tv.tag_configure('test'), dict))
+@add_standard_options(StandardTtkOptionsTests)
+class SeparatorTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'orient', 'style', 'takefocus',
+ # 'state'?
+ )
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return ttk.Separator(self.root, **kwargs)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class SizegripTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'style', 'takefocus',
+ # 'state'?
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Sizegrip(self.root, **kwargs)
+
+
tests_gui = (
- WidgetTest, ButtonTest, CheckbuttonTest, RadiobuttonTest,
- ComboboxTest, EntryTest, PanedwindowTest, ScaleTest, NotebookTest,
- TreeviewTest
+ ButtonTest, CheckbuttonTest, ComboboxTest, EntryTest,
+ FrameTest, LabelFrameTest, LabelTest, MenubuttonTest,
+ NotebookTest, PanedWindowTest, ProgressbarTest,
+ RadiobuttonTest, ScaleTest, ScrollbarTest, SeparatorTest,
+ SizegripTest, TreeviewTest, WidgetTest,
)
if __name__ == "__main__":
diff --git a/Lib/lib-tk/test/widget_tests.py b/Lib/lib-tk/test/widget_tests.py
new file mode 100644
--- /dev/null
+++ b/Lib/lib-tk/test/widget_tests.py
@@ -0,0 +1,504 @@
+# Common tests for test_tkinter/test_widgets.py and test_ttk/test_widgets.py
+
+import Tkinter
+from ttk import setup_master, Scale
+from test_ttk.support import tcl_version, requires_tcl, pixels_conv, tcl_obj_eq
+
+
+noconv = str if tcl_version < (8, 5) else False
+
+def int_round(x):
+ return int(round(x))
+
+_sentinel = object()
+
+class AbstractWidgetTest(object):
+ _conv_pixels = staticmethod(int_round) if tcl_version[:2] != (8, 5) else int
+ _conv_pad_pixels = None
+ wantobjects = True
+
+ def setUp(self):
+ self.root = setup_master()
+ self.scaling = float(self.root.call('tk', 'scaling'))
+ if not self.root.wantobjects():
+ self.wantobjects = False
+
+ def create(self, **kwargs):
+ widget = self._create(**kwargs)
+ self.addCleanup(widget.destroy)
+ return widget
+
+ def assertEqual2(self, actual, expected, msg=None, eq=object.__eq__):
+ if eq(actual, expected):
+ return
+ self.assertEqual(actual, expected, msg)
+
+ def checkParam(self, widget, name, value, expected=_sentinel,
+ conv=False, eq=None):
+ widget[name] = value
+ if expected is _sentinel:
+ expected = value
+ if conv:
+ expected = conv(expected)
+ if not self.wantobjects:
+ if isinstance(expected, tuple):
+ expected = Tkinter._join(expected)
+ else:
+ expected = str(expected)
+ if eq is None:
+ eq = tcl_obj_eq
+ self.assertEqual2(widget[name], expected, eq=eq)
+ self.assertEqual2(widget.cget(name), expected, eq=eq)
+ # XXX
+ if not isinstance(widget, Scale):
+ t = widget.configure(name)
+ self.assertEqual(len(t), 5)
+ ## XXX
+ if not isinstance(t[4], tuple):
+ self.assertEqual2(t[4], expected, eq=eq)
+
+ def checkInvalidParam(self, widget, name, value, errmsg=None,
+ keep_orig=True):
+ orig = widget[name]
+ if errmsg is not None:
+ errmsg = errmsg.format(value)
+ with self.assertRaises(Tkinter.TclError) as cm:
+ widget[name] = value
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ if keep_orig:
+ self.assertEqual(widget[name], orig)
+ else:
+ widget[name] = orig
+ with self.assertRaises(Tkinter.TclError) as cm:
+ widget.configure({name: value})
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ if keep_orig:
+ self.assertEqual(widget[name], orig)
+ else:
+ widget[name] = orig
+
+ def checkParams(self, widget, name, *values, **kwargs):
+ for value in values:
+ self.checkParam(widget, name, value, **kwargs)
+
+ def checkIntegerParam(self, widget, name, *values, **kwargs):
+ self.checkParams(widget, name, *values, **kwargs)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected integer but got ""')
+ self.checkInvalidParam(widget, name, '10p',
+ errmsg='expected integer but got "10p"')
+ self.checkInvalidParam(widget, name, 3.2,
+ errmsg='expected integer but got "3.2"')
+
+ def checkFloatParam(self, widget, name, *values, **kwargs):
+ if 'conv' in kwargs:
+ conv = kwargs.pop('conv')
+ else:
+ conv = float
+ for value in values:
+ self.checkParam(widget, name, value, conv=conv, **kwargs)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected floating-point number but got ""')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='expected floating-point number but got "spam"')
+
+ def checkBooleanParam(self, widget, name):
+ for value in (False, 0, 'false', 'no', 'off'):
+ self.checkParam(widget, name, value, expected=0)
+ for value in (True, 1, 'true', 'yes', 'on'):
+ self.checkParam(widget, name, value, expected=1)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected boolean value but got ""')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='expected boolean value but got "spam"')
+
+ def checkColorParam(self, widget, name, allow_empty=None, **kwargs):
+ self.checkParams(widget, name,
+ '#ff0000', '#00ff00', '#0000ff', '#123456',
+ 'red', 'green', 'blue', 'white', 'black', 'grey',
+ **kwargs)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='unknown color name "spam"')
+
+ def checkCursorParam(self, widget, name, **kwargs):
+ self.checkParams(widget, name, 'arrow', 'watch', 'cross', '',**kwargs)
+ if tcl_version >= (8, 5):
+ self.checkParam(widget, name, 'none')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='bad cursor spec "spam"')
+
+ def checkCommandParam(self, widget, name):
+ def command(*args):
+ pass
+ widget[name] = command
+ self.assertTrue(widget[name])
+ self.checkParams(widget, name, '')
+
+ def checkEnumParam(self, widget, name, *values, **kwargs):
+ if 'errmsg' in kwargs:
+ errmsg = kwargs.pop('errmsg')
+ else:
+ errmsg = None
+ self.checkParams(widget, name, *values, **kwargs)
+ if errmsg is None:
+ errmsg2 = ' %s "{}": must be %s%s or %s' % (
+ name,
+ ', '.join(values[:-1]),
+ ',' if len(values) > 2 else '',
+ values[-1])
+ self.checkInvalidParam(widget, name, '',
+ errmsg='ambiguous' + errmsg2)
+ errmsg = 'bad' + errmsg2
+ self.checkInvalidParam(widget, name, 'spam', errmsg=errmsg)
+
+ def checkPixelsParam(self, widget, name, *values, **kwargs):
+ if 'conv' in kwargs:
+ conv = kwargs.pop('conv')
+ else:
+ conv = None
+ if conv is None:
+ conv = self._conv_pixels
+ if 'keep_orig' in kwargs:
+ keep_orig = kwargs.pop('keep_orig')
+ else:
+ keep_orig = True
+ for value in values:
+ expected = _sentinel
+ conv1 = conv
+ if isinstance(value, str):
+ if conv1 and conv1 is not str:
+ expected = pixels_conv(value) * self.scaling
+ conv1 = int_round
+ self.checkParam(widget, name, value, expected=expected,
+ conv=conv1, **kwargs)
+ self.checkInvalidParam(widget, name, '6x',
+ errmsg='bad screen distance "6x"', keep_orig=keep_orig)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='bad screen distance "spam"', keep_orig=keep_orig)
+
+ def checkReliefParam(self, widget, name):
+ self.checkParams(widget, name,
+ 'flat', 'groove', 'raised', 'ridge', 'solid', 'sunken')
+ errmsg='bad relief "spam": must be '\
+ 'flat, groove, raised, ridge, solid, or sunken'
+ if tcl_version < (8, 6):
+ errmsg = None
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg=errmsg)
+
+ def checkImageParam(self, widget, name):
+ image = Tkinter.PhotoImage('image1')
+ self.checkParam(widget, name, image, conv=str)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='image "spam" doesn\'t exist')
+ widget[name] = ''
+
+ def checkVariableParam(self, widget, name, var):
+ self.checkParam(widget, name, var, conv=str)
+
+
+class StandardOptionsTests(object):
+ STANDARD_OPTIONS = (
+ 'activebackground', 'activeborderwidth', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
+ 'disabledforeground', 'exportselection', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'jump', 'justify', 'orient', 'padx', 'pady', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'setgrid', 'takefocus', 'text', 'textvariable', 'troughcolor',
+ 'underline', 'wraplength', 'xscrollcommand', 'yscrollcommand',
+ )
+
+ def test_activebackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'activebackground')
+
+ def test_activeborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'activeborderwidth',
+ 0, 1.3, 2.9, 6, -2, '10p')
+
+ def test_activeforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'activeforeground')
+
+ def test_anchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'anchor',
+ 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center')
+
+ def test_background(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'background')
+ if 'bg' in self.OPTIONS:
+ self.checkColorParam(widget, 'bg')
+
+ def test_bitmap(self):
+ widget = self.create()
+ self.checkParam(widget, 'bitmap', 'questhead')
+ self.checkParam(widget, 'bitmap', 'gray50')
+ self.checkInvalidParam(widget, 'bitmap', 'spam',
+ errmsg='bitmap "spam" not defined')
+
+ def test_borderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'borderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+ if 'bd' in self.OPTIONS:
+ self.checkPixelsParam(widget, 'bd', 0, 1.3, 2.6, 6, -2, '10p')
+
+ def test_compound(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'compound',
+ 'bottom', 'center', 'left', 'none', 'right', 'top')
+
+ def test_cursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'cursor')
+
+ def test_disabledforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'disabledforeground')
+
+ def test_exportselection(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'exportselection')
+
+ def test_font(self):
+ widget = self.create()
+ self.checkParam(widget, 'font',
+ '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
+ self.checkInvalidParam(widget, 'font', '',
+ errmsg='font "" doesn\'t exist')
+
+ def test_foreground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'foreground')
+ if 'fg' in self.OPTIONS:
+ self.checkColorParam(widget, 'fg')
+
+ def test_highlightbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'highlightbackground')
+
+ def test_highlightcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'highlightcolor')
+
+ def test_highlightthickness(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'highlightthickness',
+ 0, 1.3, 2.6, 6, '10p')
+ self.checkParam(widget, 'highlightthickness', -2, expected=0,
+ conv=self._conv_pixels)
+
+ def test_image(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'image')
+
+ def test_insertbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'insertbackground')
+
+ def test_insertborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertborderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+
+ def test_insertofftime(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'insertofftime', 100)
+
+ def test_insertontime(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'insertontime', 100)
+
+ def test_insertwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertwidth', 1.3, 2.6, -2, '10p')
+
+ def test_jump(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'jump')
+
+ def test_justify(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'justify', 'left', 'right', 'center',
+ errmsg='bad justification "{}": must be '
+ 'left, right, or center')
+ self.checkInvalidParam(widget, 'justify', '',
+ errmsg='ambiguous justification "": must be '
+ 'left, right, or center')
+
+ def test_orient(self):
+ widget = self.create()
+ self.assertEqual(str(widget['orient']), self.default_orient)
+ self.checkEnumParam(widget, 'orient', 'horizontal', 'vertical')
+
+ def test_padx(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, -2, '12m',
+ conv=self._conv_pad_pixels)
+
+ def test_pady(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, -2, '12m',
+ conv=self._conv_pad_pixels)
+
+ def test_relief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'relief')
+
+ def test_repeatdelay(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'repeatdelay', -500, 500)
+
+ def test_repeatinterval(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'repeatinterval', -500, 500)
+
+ def test_selectbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectbackground')
+
+ def test_selectborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'selectborderwidth', 1.3, 2.6, -2, '10p')
+
+ def test_selectforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectforeground')
+
+ def test_setgrid(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'setgrid')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'active', 'disabled', 'normal')
+
+ def test_takefocus(self):
+ widget = self.create()
+ self.checkParams(widget, 'takefocus', '0', '1', '')
+
+ def test_text(self):
+ widget = self.create()
+ self.checkParams(widget, 'text', '', 'any string')
+
+ def test_textvariable(self):
+ widget = self.create()
+ var = Tkinter.StringVar()
+ self.checkVariableParam(widget, 'textvariable', var)
+
+ def test_troughcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'troughcolor')
+
+ def test_underline(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'underline', 0, 1, 10)
+
+ def test_wraplength(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkPixelsParam(widget, 'wraplength', 100)
+ else:
+ self.checkParams(widget, 'wraplength', 100)
+
+ def test_xscrollcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'xscrollcommand')
+
+ def test_yscrollcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'yscrollcommand')
+
+ # non-standard but common options
+
+ def test_command(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'command')
+
+ def test_indicatoron(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'indicatoron')
+
+ def test_offrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'offrelief')
+
+ def test_overrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'overrelief')
+
+ def test_selectcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectcolor')
+
+ def test_selectimage(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'selectimage')
+
+ @requires_tcl(8, 5)
+ def test_tristateimage(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'tristateimage')
+
+ @requires_tcl(8, 5)
+ def test_tristatevalue(self):
+ widget = self.create()
+ self.checkParam(widget, 'tristatevalue', 'unknowable')
+
+ def test_variable(self):
+ widget = self.create()
+ var = Tkinter.DoubleVar()
+ self.checkVariableParam(widget, 'variable', var)
+
+
+class IntegerSizeTests(object):
+ def test_height(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'height', 100, -100, 0)
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402, -402, 0)
+
+
+class PixelSizeTests(object):
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '3c')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i')
+
+
+def add_standard_options(*source_classes):
+ # This decorator adds test_xxx methods from source classes for every xxx
+ # option in the OPTIONS class attribute if they are not defined explicitly.
+ def decorator(cls):
+ for option in cls.OPTIONS:
+ methodname = 'test_' + option
+ if not hasattr(cls, methodname):
+ for source_class in source_classes:
+ if hasattr(source_class, methodname):
+ setattr(cls, methodname,
+ getattr(source_class, methodname).im_func)
+ break
+ else:
+ def test(self, option=option):
+ widget = self.create()
+ widget[option]
+ raise AssertionError('Option "%s" is not tested in %s' %
+ (option, cls.__name__))
+ test.__name__ = methodname
+ setattr(cls, methodname, test)
+ return cls
+ return decorator
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,8 @@
Tests
-----
+- Issue #19085: Added basic tests for all tkinter widget options.
+
Whats' New in Python 2.7.6?
===========================
--
Repository URL: http://hg.python.org/cpython
1
0

cpython (merge 3.3 -> default): Issue #19085: Added basic tests for all tkinter widget options.
by serhiy.storchaka Nov. 2, 2013
by serhiy.storchaka Nov. 2, 2013
Nov. 2, 2013
http://hg.python.org/cpython/rev/ab7c2c1d349c
changeset: 86835:ab7c2c1d349c
parent: 86824:e071977772bf
parent: 86834:92e268f2719e
user: Serhiy Storchaka <storchaka(a)gmail.com>
date: Sat Nov 02 10:44:55 2013 +0200
summary:
Issue #19085: Added basic tests for all tkinter widget options.
files:
Lib/tkinter/test/support.py | 39 +
Lib/tkinter/test/test_tkinter/test_widgets.py | 919 ++++++++++
Lib/tkinter/test/test_ttk/test_widgets.py | 493 +++++-
Lib/tkinter/test/widget_tests.py | 487 +++++
Misc/NEWS | 2 +
5 files changed, 1917 insertions(+), 23 deletions(-)
diff --git a/Lib/tkinter/test/support.py b/Lib/tkinter/test/support.py
--- a/Lib/tkinter/test/support.py
+++ b/Lib/tkinter/test/support.py
@@ -77,3 +77,42 @@
widget.event_generate('<Motion>', x=x, y=y)
widget.event_generate('<ButtonPress-1>', x=x, y=y)
widget.event_generate('<ButtonRelease-1>', x=x, y=y)
+
+
+import _tkinter
+tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.')))
+
+def requires_tcl(*version):
+ return unittest.skipUnless(tcl_version >= version,
+ 'requires Tcl version >= ' + '.'.join(map(str, version)))
+
+units = {
+ 'c': 72 / 2.54, # centimeters
+ 'i': 72, # inches
+ 'm': 72 / 25.4, # millimeters
+ 'p': 1, # points
+}
+
+def pixels_conv(value):
+ return float(value[:-1]) * units[value[-1:]]
+
+def tcl_obj_eq(actual, expected):
+ if actual == expected:
+ return True
+ if isinstance(actual, _tkinter.Tcl_Obj):
+ if isinstance(expected, str):
+ return str(actual) == expected
+ if isinstance(actual, tuple):
+ if isinstance(expected, tuple):
+ return (len(actual) == len(expected) and
+ all(tcl_obj_eq(act, exp)
+ for act, exp in zip(actual, expected)))
+ return False
+
+def widget_eq(actual, expected):
+ if actual == expected:
+ return True
+ if isinstance(actual, (str, tkinter.Widget)):
+ if isinstance(expected, (str, tkinter.Widget)):
+ return str(actual) == str(expected)
+ return False
diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py
new file mode 100644
--- /dev/null
+++ b/Lib/tkinter/test/test_tkinter/test_widgets.py
@@ -0,0 +1,919 @@
+import unittest
+import tkinter
+import os
+from test.support import requires
+
+from tkinter.test.support import tcl_version, requires_tcl, widget_eq
+from tkinter.test.widget_tests import (add_standard_options, noconv,
+ AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests)
+
+requires('gui')
+
+
+def float_round(x):
+ return float(round(x))
+
+
+class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
+ _conv_pad_pixels = noconv
+
+ def test_class(self):
+ widget = self.create()
+ self.assertEqual(widget['class'],
+ widget.__class__.__name__.title())
+ self.checkInvalidParam(widget, 'class', 'Foo',
+ errmsg="can't modify -class option after widget is created")
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+
+ def test_colormap(self):
+ widget = self.create()
+ self.assertEqual(widget['colormap'], '')
+ self.checkInvalidParam(widget, 'colormap', 'new',
+ errmsg="can't modify -colormap option after widget is created")
+ widget2 = self.create(colormap='new')
+ self.assertEqual(widget2['colormap'], 'new')
+
+ def test_container(self):
+ widget = self.create()
+ self.assertEqual(widget['container'], 0 if self.wantobjects else '0')
+ self.checkInvalidParam(widget, 'container', 1,
+ errmsg="can't modify -container option after widget is created")
+ widget2 = self.create(container=True)
+ self.assertEqual(widget2['container'], 1 if self.wantobjects else '1')
+
+ def test_visual(self):
+ widget = self.create()
+ self.assertEqual(widget['visual'], '')
+ self.checkInvalidParam(widget, 'visual', 'default',
+ errmsg="can't modify -visual option after widget is created")
+ widget2 = self.create(visual='default')
+ self.assertEqual(widget2['visual'], 'default')
+
+
+@add_standard_options(StandardOptionsTests)
+class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'menu', 'padx', 'pady', 'relief', 'screen',
+ 'takefocus', 'use', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Toplevel(self.root, **kwargs)
+
+ def test_menu(self):
+ widget = self.create()
+ menu = tkinter.Menu(self.root)
+ self.checkParam(widget, 'menu', menu, eq=widget_eq)
+ self.checkParam(widget, 'menu', '')
+
+ def test_screen(self):
+ widget = self.create()
+ self.assertEqual(widget['screen'], '')
+ display = os.environ['DISPLAY']
+ self.checkInvalidParam(widget, 'screen', display,
+ errmsg="can't modify -screen option after widget is created")
+ widget2 = self.create(screen=display)
+ self.assertEqual(widget2['screen'], display)
+
+ def test_use(self):
+ widget = self.create()
+ self.assertEqual(widget['use'], '')
+ widget1 = self.create(container=True)
+ self.assertEqual(widget1['use'], '')
+ self.checkInvalidParam(widget1, 'use', '0x44022',
+ errmsg="can't modify -use option after widget is created")
+ wid = hex(widget1.winfo_id())
+ widget2 = self.create(use=wid)
+ self.assertEqual(widget2['use'], wid)
+
+
+@add_standard_options(StandardOptionsTests)
+class FrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'relief', 'takefocus', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Frame(self.root, **kwargs)
+
+
+@add_standard_options(StandardOptionsTests)
+class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'labelanchor', 'labelwidget', 'padx', 'pady', 'relief',
+ 'takefocus', 'text', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.LabelFrame(self.root, **kwargs)
+
+ def test_labelanchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'labelanchor',
+ 'e', 'en', 'es', 'n', 'ne', 'nw',
+ 's', 'se', 'sw', 'w', 'wn', 'ws')
+ self.checkInvalidParam(widget, 'labelanchor', 'center')
+
+ def test_labelwidget(self):
+ widget = self.create()
+ label = tkinter.Label(self.root, text='Mupp', name='foo')
+ self.checkParam(widget, 'labelwidget', label, expected='.foo')
+ label.destroy()
+
+
+class AbstractLabelTest(AbstractWidgetTest, IntegerSizeTests):
+ _conv_pixels = noconv
+
+ def test_highlightthickness(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'highlightthickness',
+ 0, 1.3, 2.6, 6, -2, '10p')
+
+
+@add_standard_options(StandardOptionsTests)
+class LabelTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'justify', 'padx', 'pady', 'relief', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Label(self.root, **kwargs)
+
+
+@add_standard_options(StandardOptionsTests)
+class ButtonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor', 'default',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'state', 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength')
+
+ def _create(self, **kwargs):
+ return tkinter.Button(self.root, **kwargs)
+
+ def test_default(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'default', 'active', 'disabled', 'normal')
+
+
+@add_standard_options(StandardOptionsTests)
+class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify',
+ 'offrelief', 'offvalue', 'onvalue', 'overrelief',
+ 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'tristateimage', 'tristatevalue',
+ 'underline', 'variable', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Checkbutton(self.root, **kwargs)
+
+
+ def test_offvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
+
+ def test_onvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
+
+
+@add_standard_options(StandardOptionsTests)
+class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify', 'offrelief', 'overrelief',
+ 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'tristateimage', 'tristatevalue',
+ 'underline', 'value', 'variable', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Radiobutton(self.root, **kwargs)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
+
+
+@add_standard_options(StandardOptionsTests)
+class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'compound', 'cursor', 'direction',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify', 'menu',
+ 'padx', 'pady', 'relief', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+ _conv_pixels = AbstractWidgetTest._conv_pixels
+
+ def _create(self, **kwargs):
+ return tkinter.Menubutton(self.root, **kwargs)
+
+ def test_direction(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'direction',
+ 'above', 'below', 'flush', 'left', 'right')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'height', 100, -100, 0, conv=str)
+
+ test_highlightthickness = StandardOptionsTests.test_highlightthickness
+
+ def test_image(self):
+ widget = self.create()
+ image = tkinter.PhotoImage('image1')
+ self.checkParam(widget, 'image', image, conv=str)
+ errmsg = 'image "spam" doesn\'t exist'
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget['image'] = 'spam'
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget.configure({'image': 'spam'})
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+
+ def test_menu(self):
+ widget = self.create()
+ menu = tkinter.Menu(widget, name='menu')
+ self.checkParam(widget, 'menu', menu, eq=widget_eq)
+ menu.destroy()
+
+ def test_padx(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, '12m')
+ self.checkParam(widget, 'padx', -2, expected=0)
+
+ def test_pady(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, '12m')
+ self.checkParam(widget, 'pady', -2, expected=0)
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402, -402, 0, conv=str)
+
+
+class OptionMenuTest(MenubuttonTest, unittest.TestCase):
+
+ def _create(self, default='b', values=('a', 'b', 'c'), **kwargs):
+ return tkinter.OptionMenu(self.root, None, default, *values, **kwargs)
+
+
+@add_standard_options(IntegerSizeTests, StandardOptionsTests)
+class EntryTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth', 'cursor',
+ 'disabledbackground', 'disabledforeground',
+ 'exportselection', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'invalidcommand', 'justify', 'readonlybackground', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'show', 'state', 'takefocus', 'textvariable',
+ 'validate', 'validatecommand', 'width', 'xscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Entry(self.root, **kwargs)
+
+ def test_disabledbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'disabledbackground')
+
+ def test_insertborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertborderwidth', 0, 1.3, -2)
+ self.checkParam(widget, 'insertborderwidth', 2, expected=1)
+ self.checkParam(widget, 'insertborderwidth', '10p', expected=1)
+
+ def test_insertwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertwidth', 1.3, 3.6, '10p')
+ if tcl_version[:2] == (8, 5):
+ self.checkParam(widget, 'insertwidth', 0.9, expected=2)
+ else:
+ self.checkParam(widget, 'insertwidth', 0.9, expected=1)
+ self.checkParam(widget, 'insertwidth', 0.1, expected=2)
+ self.checkParam(widget, 'insertwidth', -2, expected=2)
+
+ def test_invalidcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'invalidcommand')
+ self.checkCommandParam(widget, 'invcmd')
+
+ def test_readonlybackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'readonlybackground')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', '*')
+ self.checkParam(widget, 'show', '')
+ self.checkParam(widget, 'show', ' ')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state',
+ 'disabled', 'normal', 'readonly')
+
+ def test_validate(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'validate',
+ 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
+
+ def test_validatecommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'validatecommand')
+ self.checkCommandParam(widget, 'vcmd')
+
+
+@add_standard_options(StandardOptionsTests)
+class SpinboxTest(EntryTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'background', 'borderwidth',
+ 'buttonbackground', 'buttoncursor', 'buttondownrelief', 'buttonuprelief',
+ 'command', 'cursor', 'disabledbackground', 'disabledforeground',
+ 'exportselection', 'font', 'foreground', 'format', 'from',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'increment',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'invalidcommand', 'justify', 'relief', 'readonlybackground',
+ 'repeatdelay', 'repeatinterval',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'state', 'takefocus', 'textvariable', 'to',
+ 'validate', 'validatecommand', 'values',
+ 'width', 'wrap', 'xscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Spinbox(self.root, **kwargs)
+
+ test_show = None
+
+ def test_buttonbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'buttonbackground')
+
+ def test_buttoncursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'buttoncursor')
+
+ def test_buttondownrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'buttondownrelief')
+
+ def test_buttonuprelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'buttonuprelief')
+
+ def test_format(self):
+ widget = self.create()
+ self.checkParam(widget, 'format', '%2f')
+ self.checkParam(widget, 'format', '%2.2f')
+ self.checkParam(widget, 'format', '%.2f')
+ self.checkParam(widget, 'format', '%2.f')
+ self.checkInvalidParam(widget, 'format', '%2e-1f')
+ self.checkInvalidParam(widget, 'format', '2.2')
+ self.checkInvalidParam(widget, 'format', '%2.-2f')
+ self.checkParam(widget, 'format', '%-2.02f')
+ self.checkParam(widget, 'format', '% 2.02f')
+ self.checkParam(widget, 'format', '% -2.200f')
+ self.checkParam(widget, 'format', '%09.200f')
+ self.checkInvalidParam(widget, 'format', '%d')
+
+ def test_from(self):
+ widget = self.create()
+ self.checkParam(widget, 'to', 100.0)
+ self.checkFloatParam(widget, 'from', -10, 10.2, 11.7)
+ self.checkInvalidParam(widget, 'from', 200,
+ errmsg='-to value must be greater than -from value')
+
+ def test_increment(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'increment', -1, 1, 10.2, 12.8, 0)
+
+ def test_to(self):
+ widget = self.create()
+ self.checkParam(widget, 'from', -100.0)
+ self.checkFloatParam(widget, 'to', -10, 10.2, 11.7)
+ self.checkInvalidParam(widget, 'to', -200,
+ errmsg='-to value must be greater than -from value')
+
+ def test_values(self):
+ # XXX
+ widget = self.create()
+ self.assertEqual(widget['values'], '')
+ self.checkParam(widget, 'values', 'mon tue wed thur')
+ self.checkParam(widget, 'values', ('mon', 'tue', 'wed', 'thur'),
+ expected='mon tue wed thur')
+ self.checkParam(widget, 'values', (42, 3.14, '', 'any string'),
+ expected='42 3.14 {} {any string}')
+ self.checkParam(widget, 'values', '')
+
+ def test_wrap(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'wrap')
+
+
+@add_standard_options(StandardOptionsTests)
+class TextTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'autoseparators', 'background', 'blockcursor', 'borderwidth',
+ 'cursor', 'endline', 'exportselection',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'inactiveselectbackground', 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertunfocussed', 'insertwidth',
+ 'maxundo', 'padx', 'pady', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'setgrid', 'spacing1', 'spacing2', 'spacing3', 'startline', 'state',
+ 'tabs', 'tabstyle', 'takefocus', 'undo', 'width', 'wrap',
+ 'xscrollcommand', 'yscrollcommand',
+ )
+ if tcl_version < (8, 5):
+ wantobjects = False
+
+ def _create(self, **kwargs):
+ return tkinter.Text(self.root, **kwargs)
+
+ def test_autoseparators(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'autoseparators')
+
+ @requires_tcl(8, 5)
+ def test_blockcursor(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'blockcursor')
+
+ @requires_tcl(8, 5)
+ def test_endline(self):
+ widget = self.create()
+ text = '\n'.join('Line %d' for i in range(100))
+ widget.insert('end', text)
+ self.checkParam(widget, 'endline', 200, expected='')
+ self.checkParam(widget, 'endline', -10, expected='')
+ self.checkInvalidParam(widget, 'endline', 'spam',
+ errmsg='expected integer but got "spam"')
+ self.checkParam(widget, 'endline', 50)
+ self.checkParam(widget, 'startline', 15)
+ self.checkInvalidParam(widget, 'endline', 10,
+ errmsg='-startline must be less than or equal to -endline')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, '3c')
+ self.checkParam(widget, 'height', -100, expected=1)
+ self.checkParam(widget, 'height', 0, expected=1)
+
+ def test_maxundo(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'maxundo', 0, 5, -1)
+
+ @requires_tcl(8, 5)
+ def test_inactiveselectbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'inactiveselectbackground')
+
+ @requires_tcl(8, 6)
+ def test_insertunfocussed(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'insertunfocussed',
+ 'hollow', 'none', 'solid')
+
+ def test_selectborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'selectborderwidth',
+ 1.3, 2.6, -2, '10p', conv=False,
+ keep_orig=tcl_version >= (8, 5))
+
+ def test_spacing1(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing1', 20, 21.4, 22.6, '0.5c')
+ self.checkParam(widget, 'spacing1', -5, expected=0)
+
+ def test_spacing2(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing2', 5, 6.4, 7.6, '0.1c')
+ self.checkParam(widget, 'spacing2', -1, expected=0)
+
+ def test_spacing3(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing3', 20, 21.4, 22.6, '0.5c')
+ self.checkParam(widget, 'spacing3', -10, expected=0)
+
+ @requires_tcl(8, 5)
+ def test_startline(self):
+ widget = self.create()
+ text = '\n'.join('Line %d' for i in range(100))
+ widget.insert('end', text)
+ self.checkParam(widget, 'startline', 200, expected='')
+ self.checkParam(widget, 'startline', -10, expected='')
+ self.checkInvalidParam(widget, 'startline', 'spam',
+ errmsg='expected integer but got "spam"')
+ self.checkParam(widget, 'startline', 10)
+ self.checkParam(widget, 'endline', 50)
+ self.checkInvalidParam(widget, 'startline', 70,
+ errmsg='-startline must be less than or equal to -endline')
+
+ def test_state(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkParams(widget, 'state', 'disabled', 'normal')
+ else:
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal')
+
+ def test_tabs(self):
+ widget = self.create()
+ self.checkParam(widget, 'tabs', (10.2, 20.7, '1i', '2i'))
+ self.checkParam(widget, 'tabs', '10.2 20.7 1i 2i',
+ expected=('10.2', '20.7', '1i', '2i'))
+ self.checkParam(widget, 'tabs', '2c left 4c 6c center',
+ expected=('2c', 'left', '4c', '6c', 'center'))
+ self.checkInvalidParam(widget, 'tabs', 'spam',
+ errmsg='bad screen distance "spam"',
+ keep_orig=tcl_version >= (8, 5))
+
+ @requires_tcl(8, 5)
+ def test_tabstyle(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'tabstyle', 'tabular', 'wordprocessor')
+
+ def test_undo(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'undo')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402)
+ self.checkParam(widget, 'width', -402, expected=1)
+ self.checkParam(widget, 'width', 0, expected=1)
+
+ def test_wrap(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkParams(widget, 'wrap', 'char', 'none', 'word')
+ else:
+ self.checkEnumParam(widget, 'wrap', 'char', 'none', 'word')
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class CanvasTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'closeenough', 'confine', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'relief', 'scrollregion',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'state', 'takefocus',
+ 'xscrollcommand', 'xscrollincrement',
+ 'yscrollcommand', 'yscrollincrement', 'width',
+ )
+
+ _conv_pixels = round
+ wantobjects = False
+
+ def _create(self, **kwargs):
+ return tkinter.Canvas(self.root, **kwargs)
+
+ def test_closeenough(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'closeenough', 24, 2.4, 3.6, -3,
+ conv=float)
+
+ def test_confine(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'confine')
+
+ def test_scrollregion(self):
+ widget = self.create()
+ self.checkParam(widget, 'scrollregion', '0 0 200 150')
+ self.checkParam(widget, 'scrollregion', (0, 0, 200, 150),
+ expected='0 0 200 150')
+ self.checkParam(widget, 'scrollregion', '')
+ self.checkInvalidParam(widget, 'scrollregion', 'spam',
+ errmsg='bad scrollRegion "spam"')
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 'spam'))
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200))
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 150, 0))
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal',
+ errmsg='bad state value "{}": must be normal or disabled')
+
+ def test_xscrollincrement(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'xscrollincrement',
+ 40, 0, 41.2, 43.6, -40, '0.5i')
+
+ def test_yscrollincrement(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'yscrollincrement',
+ 10, 0, 11.2, 13.6, -10, '0.1i')
+
+
+@add_standard_options(IntegerSizeTests, StandardOptionsTests)
+class ListboxTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activestyle', 'background', 'borderwidth', 'cursor',
+ 'disabledforeground', 'exportselection',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'listvariable', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'selectmode', 'setgrid', 'state',
+ 'takefocus', 'width', 'xscrollcommand', 'yscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Listbox(self.root, **kwargs)
+
+ def test_activestyle(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'activestyle',
+ 'dotbox', 'none', 'underline')
+
+ def test_listvariable(self):
+ widget = self.create()
+ var = tkinter.DoubleVar()
+ self.checkVariableParam(widget, 'listvariable', var)
+
+ def test_selectmode(self):
+ widget = self.create()
+ self.checkParam(widget, 'selectmode', 'single')
+ self.checkParam(widget, 'selectmode', 'browse')
+ self.checkParam(widget, 'selectmode', 'multiple')
+ self.checkParam(widget, 'selectmode', 'extended')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal')
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class ScaleTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'background', 'bigincrement', 'borderwidth',
+ 'command', 'cursor', 'digits', 'font', 'foreground', 'from',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'label', 'length', 'orient', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'resolution', 'showvalue', 'sliderlength', 'sliderrelief', 'state',
+ 'takefocus', 'tickinterval', 'to', 'troughcolor', 'variable', 'width',
+ )
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return tkinter.Scale(self.root, **kwargs)
+
+ def test_bigincrement(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'bigincrement', 12.4, 23.6, -5)
+
+ def test_digits(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'digits', 5, 0)
+
+ def test_from(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=float_round)
+
+ def test_label(self):
+ widget = self.create()
+ self.checkParam(widget, 'label', 'any string')
+ self.checkParam(widget, 'label', '')
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
+
+ def test_resolution(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'resolution', 4.2, 0, 6.7, -2)
+
+ def test_showvalue(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'showvalue')
+
+ def test_sliderlength(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sliderlength',
+ 10, 11.2, 15.6, -3, '3m')
+
+ def test_sliderrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'sliderrelief')
+
+ def test_tickinterval(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'tickinterval', 1, 4.3, 7.6, 0,
+ conv=float_round)
+ self.checkParam(widget, 'tickinterval', -2, expected=2,
+ conv=float_round)
+
+ def test_to(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10,
+ conv=float_round)
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activerelief',
+ 'background', 'borderwidth',
+ 'command', 'cursor', 'elementborderwidth',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'jump', 'orient', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'takefocus', 'troughcolor', 'width',
+ )
+ _conv_pixels = round
+ wantobjects = False
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return tkinter.Scrollbar(self.root, **kwargs)
+
+ def test_activerelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'activerelief')
+
+ def test_elementborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'elementborderwidth', 4.3, 5.6, -2, '1m')
+
+ def test_orient(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'orient', 'vertical', 'horizontal',
+ errmsg='bad orientation "{}": must be vertical or horizontal')
+
+
+@add_standard_options(StandardOptionsTests)
+class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth', 'cursor',
+ 'handlepad', 'handlesize', 'height',
+ 'opaqueresize', 'orient', 'relief',
+ 'sashcursor', 'sashpad', 'sashrelief', 'sashwidth',
+ 'showhandle', 'width',
+ )
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return tkinter.PanedWindow(self.root, **kwargs)
+
+ def test_handlepad(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'handlepad', 5, 6.4, 7.6, -3, '1m')
+
+ def test_handlesize(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'handlesize', 8, 9.4, 10.6, -3, '2m',
+ conv=noconv)
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i',
+ conv=noconv)
+
+ def test_opaqueresize(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'opaqueresize')
+
+ def test_sashcursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'sashcursor')
+
+ def test_sashpad(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sashpad', 8, 1.3, 2.6, -2, '2m')
+
+ def test_sashrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'sashrelief')
+
+ def test_sashwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sashwidth', 10, 11.1, 15.6, -3, '1m',
+ conv=noconv)
+
+ def test_showhandle(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'showhandle')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i',
+ conv=noconv)
+
+
+@add_standard_options(StandardOptionsTests)
+class MenuTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeborderwidth', 'activeforeground',
+ 'background', 'borderwidth', 'cursor',
+ 'disabledforeground', 'font', 'foreground',
+ 'postcommand', 'relief', 'selectcolor', 'takefocus',
+ 'tearoff', 'tearoffcommand', 'title', 'type',
+ )
+ _conv_pixels = noconv
+
+ def _create(self, **kwargs):
+ return tkinter.Menu(self.root, **kwargs)
+
+ def test_postcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'postcommand')
+
+ def test_tearoff(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'tearoff')
+
+ def test_tearoffcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'tearoffcommand')
+
+ def test_title(self):
+ widget = self.create()
+ self.checkParam(widget, 'title', 'any string')
+
+ def test_type(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'type',
+ 'normal', 'tearoff', 'menubar')
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class MessageTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'anchor', 'aspect', 'background', 'borderwidth',
+ 'cursor', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'justify', 'padx', 'pady', 'relief',
+ 'takefocus', 'text', 'textvariable', 'width',
+ )
+ _conv_pad_pixels = noconv
+
+ def _create(self, **kwargs):
+ return tkinter.Message(self.root, **kwargs)
+
+ def test_aspect(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'aspect', 250, 0, -300)
+
+
+tests_gui = (
+ ButtonTest, CanvasTest, CheckbuttonTest, EntryTest,
+ FrameTest, LabelFrameTest,LabelTest, ListboxTest,
+ MenubuttonTest, MenuTest, MessageTest, OptionMenuTest,
+ PanedWindowTest, RadiobuttonTest, ScaleTest, ScrollbarTest,
+ SpinboxTest, TextTest, ToplevelTest,
+)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/tkinter/test/test_ttk/test_widgets.py b/Lib/tkinter/test/test_ttk/test_widgets.py
--- a/Lib/tkinter/test/test_ttk/test_widgets.py
+++ b/Lib/tkinter/test/test_ttk/test_widgets.py
@@ -1,15 +1,57 @@
import unittest
import tkinter
-import os
from tkinter import ttk
-from test.support import requires, run_unittest
+from test.support import requires
import sys
import tkinter.test.support as support
-from tkinter.test.test_ttk.test_functions import MockTclObj, MockStateSpec
+from tkinter.test.test_ttk.test_functions import MockTclObj
+from tkinter.test.support import tcl_version
+from tkinter.test.widget_tests import (add_standard_options, noconv,
+ AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests)
requires('gui')
+
+class StandardTtkOptionsTests(StandardOptionsTests):
+
+ def test_class(self):
+ widget = self.create()
+ self.assertEqual(widget['class'], '')
+ errmsg='attempt to change read-only option'
+ if tcl_version < (8, 6):
+ errmsg='Attempt to change read-only option'
+ self.checkInvalidParam(widget, 'class', 'Foo', errmsg=errmsg)
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+
+ def test_padding(self):
+ widget = self.create()
+ self.checkParam(widget, 'padding', 0, expected=('0',))
+ self.checkParam(widget, 'padding', 5, expected=('5',))
+ self.checkParam(widget, 'padding', (5, 6), expected=('5', '6'))
+ self.checkParam(widget, 'padding', (5, 6, 7),
+ expected=('5', '6', '7'))
+ self.checkParam(widget, 'padding', (5, 6, 7, 8),
+ expected=('5', '6', '7', '8'))
+ self.checkParam(widget, 'padding', ('5p', '6p', '7p', '8p'))
+ self.checkParam(widget, 'padding', (), expected='')
+
+ def test_style(self):
+ widget = self.create()
+ self.assertEqual(widget['style'], '')
+ errmsg = 'Layout Foo not found'
+ if hasattr(self, 'default_orient'):
+ errmsg = ('Layout %s.Foo not found' %
+ getattr(self, 'default_orient').title())
+ self.checkInvalidParam(widget, 'style', 'Foo',
+ errmsg=errmsg)
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+ # XXX
+ pass
+
+
class WidgetTest(unittest.TestCase):
"""Tests methods available in every ttk widget."""
@@ -73,7 +115,112 @@
self.assertEqual(self.widget.state(), ('active', ))
-class ButtonTest(unittest.TestCase):
+class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
+ _conv_pixels = noconv
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class FrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'borderwidth', 'class', 'cursor', 'height',
+ 'padding', 'relief', 'style', 'takefocus',
+ 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Frame(self.root, **kwargs)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'borderwidth', 'class', 'cursor', 'height',
+ 'labelanchor', 'labelwidget',
+ 'padding', 'relief', 'style', 'takefocus',
+ 'text', 'underline', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.LabelFrame(self.root, **kwargs)
+
+ def test_labelanchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'labelanchor',
+ 'e', 'en', 'es', 'n', 'ne', 'nw', 's', 'se', 'sw', 'w', 'wn', 'ws',
+ errmsg='Bad label anchor specification {}')
+ self.checkInvalidParam(widget, 'labelanchor', 'center')
+
+ def test_labelwidget(self):
+ widget = self.create()
+ label = ttk.Label(self.root, text='Mupp', name='foo')
+ self.checkParam(widget, 'labelwidget', label, expected='.foo')
+ label.destroy()
+
+
+class AbstractLabelTest(AbstractWidgetTest):
+
+ def checkImageParam(self, widget, name):
+ image = tkinter.PhotoImage('image1')
+ image2 = tkinter.PhotoImage('image2')
+ self.checkParam(widget, name, image, expected=('image1',))
+ self.checkParam(widget, name, 'image1', expected=('image1',))
+ self.checkParam(widget, name, (image,), expected=('image1',))
+ self.checkParam(widget, name, (image, 'active', image2),
+ expected=('image1', 'active', 'image2'))
+ self.checkParam(widget, name, 'image1 active image2',
+ expected=('image1', 'active', 'image2'))
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='image "spam" doesn\'t exist')
+
+ def test_compound(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'compound',
+ 'none', 'text', 'image', 'center',
+ 'top', 'bottom', 'left', 'right')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkParams(widget, 'width', 402, -402, 0)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class LabelTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'anchor', 'background',
+ 'class', 'compound', 'cursor', 'font', 'foreground',
+ 'image', 'justify', 'padding', 'relief', 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+ _conv_pixels = noconv
+
+ def _create(self, **kwargs):
+ return ttk.Label(self.root, **kwargs)
+
+ def test_font(self):
+ widget = self.create()
+ self.checkParam(widget, 'font',
+ '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class ButtonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor', 'default',
+ 'image', 'state', 'style', 'takefocus', 'text', 'textvariable',
+ 'underline', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Button(self.root, **kwargs)
+
+ def test_default(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'default', 'normal', 'active', 'disabled')
def test_invoke(self):
success = []
@@ -82,7 +229,27 @@
self.assertTrue(success)
-class CheckbuttonTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor',
+ 'image',
+ 'offvalue', 'onvalue',
+ 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'variable', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Checkbutton(self.root, **kwargs)
+
+ def test_offvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
+
+ def test_onvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
def test_invoke(self):
success = []
@@ -105,21 +272,40 @@
cbtn['command'] = ''
res = cbtn.invoke()
- self.assertEqual(str(res), '')
+ self.assertFalse(str(res))
self.assertFalse(len(success) > 1)
self.assertEqual(cbtn['offvalue'],
cbtn.tk.globalgetvar(cbtn['variable']))
-class ComboboxTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class ComboboxTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'exportselection', 'height',
+ 'justify', 'postcommand', 'state', 'style',
+ 'takefocus', 'textvariable', 'values', 'width',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.combo = ttk.Combobox()
+ self.combo = self.create()
def tearDown(self):
self.combo.destroy()
support.root_withdraw()
+ super().tearDown()
+
+ def _create(self, **kwargs):
+ return ttk.Combobox(self.root, **kwargs)
+
+ def test_height(self):
+ widget = self.create()
+ self.checkParams(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
def _show_drop_down_listbox(self):
width = self.combo.winfo_width()
@@ -167,8 +353,16 @@
self.assertEqual(self.combo.get(), getval)
self.assertEqual(self.combo.current(), currval)
+ self.assertEqual(self.combo['values'],
+ () if tcl_version < (8, 5) else '')
check_get_current('', -1)
+ self.checkParam(self.combo, 'values', 'mon tue wed thur',
+ expected=('mon', 'tue', 'wed', 'thur'))
+ self.checkParam(self.combo, 'values', ('mon', 'tue', 'wed', 'thur'))
+ self.checkParam(self.combo, 'values', (42, 3.14, '', 'any string'))
+ self.checkParam(self.combo, 'values', '', expected=())
+
self.combo['values'] = ['a', 1, 'c']
self.combo.set('c')
@@ -209,15 +403,52 @@
combo2.destroy()
-class EntryTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class EntryTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'class', 'cursor',
+ 'exportselection', 'font',
+ 'invalidcommand', 'justify',
+ 'show', 'state', 'style', 'takefocus', 'textvariable',
+ 'validate', 'validatecommand', 'width', 'xscrollcommand',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.entry = ttk.Entry()
+ self.entry = self.create()
def tearDown(self):
self.entry.destroy()
support.root_withdraw()
+ super().tearDown()
+
+ def _create(self, **kwargs):
+ return ttk.Entry(self.root, **kwargs)
+
+ def test_invalidcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'invalidcommand')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', '*')
+ self.checkParam(widget, 'show', '')
+ self.checkParam(widget, 'show', ' ')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state',
+ 'disabled', 'normal', 'readonly')
+
+ def test_validate(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'validate',
+ 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
+
+ def test_validatecommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'validatecommand')
def test_bbox(self):
@@ -313,16 +544,36 @@
self.assertEqual(self.entry.state(), ())
-class PanedwindowTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'height',
+ 'orient', 'style', 'takefocus', 'width',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.paned = ttk.Panedwindow()
+ self.paned = self.create()
def tearDown(self):
self.paned.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.PanedWindow(self.root, **kwargs)
+
+ def test_orient(self):
+ widget = self.create()
+ self.assertEqual(str(widget['orient']), 'vertical')
+ errmsg='attempt to change read-only option'
+ if tcl_version < (8, 6):
+ errmsg='Attempt to change read-only option'
+ self.checkInvalidParam(widget, 'orient', 'horizontal',
+ errmsg=errmsg)
+ widget2 = self.create(orient='horizontal')
+ self.assertEqual(str(widget2['orient']), 'horizontal')
def test_add(self):
# attempt to add a child that is not a direct child of the paned window
@@ -432,7 +683,22 @@
self.assertTrue(isinstance(self.paned.sashpos(0), int))
-class RadiobuttonTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor',
+ 'image',
+ 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'value', 'variable', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Radiobutton(self.root, **kwargs)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
def test_invoke(self):
success = []
@@ -462,19 +728,68 @@
self.assertEqual(str(cbtn['variable']), str(cbtn2['variable']))
+class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'compound', 'cursor', 'direction',
+ 'image', 'menu', 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width',
+ )
-class ScaleTest(unittest.TestCase):
+ def _create(self, **kwargs):
+ return ttk.Menubutton(self.root, **kwargs)
+
+ def test_direction(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'direction',
+ 'above', 'below', 'left', 'right', 'flush')
+
+ def test_menu(self):
+ widget = self.create()
+ menu = tkinter.Menu(widget, name='menu')
+ self.checkParam(widget, 'menu', menu, conv=str)
+ menu.destroy()
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class ScaleTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'cursor', 'from', 'length',
+ 'orient', 'style', 'takefocus', 'to', 'value', 'variable',
+ )
+ _conv_pixels = noconv
+ default_orient = 'horizontal'
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.scale = ttk.Scale()
+ self.scale = self.create()
self.scale.pack()
self.scale.update()
def tearDown(self):
self.scale.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.Scale(self.root, **kwargs)
+
+ def test_from(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=False)
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
+
+ def test_to(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10, conv=False)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'value', 300, 14.9, 15.1, -10, conv=False)
def test_custom_event(self):
failure = [1, 1, 1] # will need to be empty
@@ -539,11 +854,64 @@
self.assertRaises(tkinter.TclError, self.scale.set, None)
-class NotebookTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class ProgressbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'orient', 'length',
+ 'mode', 'maximum', 'phase',
+ 'style', 'takefocus', 'value', 'variable',
+ )
+ _conv_pixels = noconv
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return ttk.Progressbar(self.root, **kwargs)
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 100.1, 56.7, '2i')
+
+ def test_maximum(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'maximum', 150.2, 77.7, 0, -10, conv=False)
+
+ def test_mode(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'mode', 'determinate', 'indeterminate')
+
+ def test_phase(self):
+ # XXX
+ pass
+
+ def test_value(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'value', 150.2, 77.7, 0, -10,
+ conv=False)
+
+
+(a)unittest.skipIf(sys.platform == 'darwin',
+ 'ttk.Scrollbar is special on MacOSX')
+@add_standard_options(StandardTtkOptionsTests)
+class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'cursor', 'orient', 'style', 'takefocus',
+ )
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return ttk.Scrollbar(self.root, **kwargs)
+
+
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class NotebookTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'height', 'padding', 'style', 'takefocus',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.nb = ttk.Notebook(padding=0)
+ self.nb = self.create(padding=0)
self.child1 = ttk.Label()
self.child2 = ttk.Label()
self.nb.add(self.child1, text='a')
@@ -554,7 +922,10 @@
self.child2.destroy()
self.nb.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.Notebook(self.root, **kwargs)
def test_tab_identifiers(self):
self.nb.forget(0)
@@ -746,16 +1117,68 @@
self.assertEqual(self.nb.select(), str(self.child1))
-class TreeviewTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'columns', 'cursor', 'displaycolumns',
+ 'height', 'padding', 'selectmode', 'show',
+ 'style', 'takefocus', 'xscrollcommand', 'yscrollcommand',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.tv = ttk.Treeview(padding=0)
+ self.tv = self.create(padding=0)
def tearDown(self):
self.tv.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.Treeview(self.root, **kwargs)
+
+ def test_columns(self):
+ widget = self.create()
+ self.checkParam(widget, 'columns', 'a b c',
+ expected=('a', 'b', 'c'))
+ self.checkParam(widget, 'columns', ('a', 'b', 'c'))
+ self.checkParam(widget, 'columns', ())
+
+ def test_displaycolumns(self):
+ widget = self.create()
+ widget['columns'] = ('a', 'b', 'c')
+ self.checkParam(widget, 'displaycolumns', 'b a c',
+ expected=('b', 'a', 'c'))
+ self.checkParam(widget, 'displaycolumns', ('b', 'a', 'c'))
+ self.checkParam(widget, 'displaycolumns', '#all',
+ expected=('#all',))
+ self.checkParam(widget, 'displaycolumns', (2, 1, 0))
+ self.checkInvalidParam(widget, 'displaycolumns', ('a', 'b', 'd'),
+ errmsg='Invalid column index d')
+ self.checkInvalidParam(widget, 'displaycolumns', (1, 2, 3),
+ errmsg='Column index 3 out of bounds')
+ self.checkInvalidParam(widget, 'displaycolumns', (1, -2),
+ errmsg='Column index -2 out of bounds')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, -100, 0, '3c', conv=False)
+ self.checkPixelsParam(widget, 'height', 101.2, 102.6, conv=noconv)
+
+ def test_selectmode(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'selectmode',
+ 'none', 'browse', 'extended')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', 'tree headings',
+ expected=('tree', 'headings'))
+ self.checkParam(widget, 'show', ('tree', 'headings'))
+ self.checkParam(widget, 'show', ('headings', 'tree'))
+ self.checkParam(widget, 'show', 'tree', expected=('tree',))
+ self.checkParam(widget, 'show', 'headings', expected=('headings',))
def test_bbox(self):
self.tv.pack()
@@ -1149,11 +1572,35 @@
self.assertTrue(isinstance(self.tv.tag_configure('test'), dict))
+@add_standard_options(StandardTtkOptionsTests)
+class SeparatorTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'orient', 'style', 'takefocus',
+ # 'state'?
+ )
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return ttk.Separator(self.root, **kwargs)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class SizegripTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'style', 'takefocus',
+ # 'state'?
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Sizegrip(self.root, **kwargs)
+
tests_gui = (
- WidgetTest, ButtonTest, CheckbuttonTest, RadiobuttonTest,
- ComboboxTest, EntryTest, PanedwindowTest, ScaleTest, NotebookTest,
- TreeviewTest
+ ButtonTest, CheckbuttonTest, ComboboxTest, EntryTest,
+ FrameTest, LabelFrameTest, LabelTest, MenubuttonTest,
+ NotebookTest, PanedWindowTest, ProgressbarTest,
+ RadiobuttonTest, ScaleTest, ScrollbarTest, SeparatorTest,
+ SizegripTest, TreeviewTest, WidgetTest,
)
if __name__ == "__main__":
- run_unittest(*tests_gui)
+ unittest.main()
diff --git a/Lib/tkinter/test/widget_tests.py b/Lib/tkinter/test/widget_tests.py
new file mode 100644
--- /dev/null
+++ b/Lib/tkinter/test/widget_tests.py
@@ -0,0 +1,487 @@
+# Common tests for test_tkinter/test_widgets.py and test_ttk/test_widgets.py
+
+import tkinter
+from tkinter.ttk import setup_master, Scale
+from tkinter.test.support import (tcl_version, requires_tcl, pixels_conv,
+ tcl_obj_eq)
+
+
+noconv = str if tcl_version < (8, 5) else False
+
+_sentinel = object()
+
+class AbstractWidgetTest:
+ _conv_pixels = round if tcl_version[:2] != (8, 5) else int
+ _conv_pad_pixels = None
+ wantobjects = True
+
+ def setUp(self):
+ self.root = setup_master()
+ self.scaling = float(self.root.call('tk', 'scaling'))
+ if not self.root.wantobjects():
+ self.wantobjects = False
+
+ def create(self, **kwargs):
+ widget = self._create(**kwargs)
+ self.addCleanup(widget.destroy)
+ return widget
+
+ def assertEqual2(self, actual, expected, msg=None, eq=object.__eq__):
+ if eq(actual, expected):
+ return
+ self.assertEqual(actual, expected, msg)
+
+ def checkParam(self, widget, name, value, *, expected=_sentinel,
+ conv=False, eq=None):
+ widget[name] = value
+ if expected is _sentinel:
+ expected = value
+ if conv:
+ expected = conv(expected)
+ if not self.wantobjects:
+ if isinstance(expected, tuple):
+ expected = tkinter._join(expected)
+ else:
+ expected = str(expected)
+ if eq is None:
+ eq = tcl_obj_eq
+ self.assertEqual2(widget[name], expected, eq=eq)
+ self.assertEqual2(widget.cget(name), expected, eq=eq)
+ # XXX
+ if not isinstance(widget, Scale):
+ t = widget.configure(name)
+ self.assertEqual(len(t), 5)
+ ## XXX
+ if not isinstance(t[4], tuple):
+ self.assertEqual2(t[4], expected, eq=eq)
+
+ def checkInvalidParam(self, widget, name, value, errmsg=None, *,
+ keep_orig=True):
+ orig = widget[name]
+ if errmsg is not None:
+ errmsg = errmsg.format(value)
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget[name] = value
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ if keep_orig:
+ self.assertEqual(widget[name], orig)
+ else:
+ widget[name] = orig
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget.configure({name: value})
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ if keep_orig:
+ self.assertEqual(widget[name], orig)
+ else:
+ widget[name] = orig
+
+ def checkParams(self, widget, name, *values, **kwargs):
+ for value in values:
+ self.checkParam(widget, name, value, **kwargs)
+
+ def checkIntegerParam(self, widget, name, *values, **kwargs):
+ self.checkParams(widget, name, *values, **kwargs)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected integer but got ""')
+ self.checkInvalidParam(widget, name, '10p',
+ errmsg='expected integer but got "10p"')
+ self.checkInvalidParam(widget, name, 3.2,
+ errmsg='expected integer but got "3.2"')
+
+ def checkFloatParam(self, widget, name, *values, conv=float, **kwargs):
+ for value in values:
+ self.checkParam(widget, name, value, conv=conv, **kwargs)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected floating-point number but got ""')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='expected floating-point number but got "spam"')
+
+ def checkBooleanParam(self, widget, name):
+ for value in (False, 0, 'false', 'no', 'off'):
+ self.checkParam(widget, name, value, expected=0)
+ for value in (True, 1, 'true', 'yes', 'on'):
+ self.checkParam(widget, name, value, expected=1)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected boolean value but got ""')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='expected boolean value but got "spam"')
+
+ def checkColorParam(self, widget, name, *, allow_empty=None, **kwargs):
+ self.checkParams(widget, name,
+ '#ff0000', '#00ff00', '#0000ff', '#123456',
+ 'red', 'green', 'blue', 'white', 'black', 'grey',
+ **kwargs)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='unknown color name "spam"')
+
+ def checkCursorParam(self, widget, name, **kwargs):
+ self.checkParams(widget, name, 'arrow', 'watch', 'cross', '',**kwargs)
+ if tcl_version >= (8, 5):
+ self.checkParam(widget, name, 'none')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='bad cursor spec "spam"')
+
+ def checkCommandParam(self, widget, name):
+ def command(*args):
+ pass
+ widget[name] = command
+ self.assertTrue(widget[name])
+ self.checkParams(widget, name, '')
+
+ def checkEnumParam(self, widget, name, *values, errmsg=None, **kwargs):
+ self.checkParams(widget, name, *values, **kwargs)
+ if errmsg is None:
+ errmsg2 = ' %s "{}": must be %s%s or %s' % (
+ name,
+ ', '.join(values[:-1]),
+ ',' if len(values) > 2 else '',
+ values[-1])
+ self.checkInvalidParam(widget, name, '',
+ errmsg='ambiguous' + errmsg2)
+ errmsg = 'bad' + errmsg2
+ self.checkInvalidParam(widget, name, 'spam', errmsg=errmsg)
+
+ def checkPixelsParam(self, widget, name, *values,
+ conv=None, keep_orig=True, **kwargs):
+ if conv is None:
+ conv = self._conv_pixels
+ for value in values:
+ expected = _sentinel
+ conv1 = conv
+ if isinstance(value, str):
+ if conv1 and conv1 is not str:
+ expected = pixels_conv(value) * self.scaling
+ conv1 = round
+ self.checkParam(widget, name, value, expected=expected,
+ conv=conv1, **kwargs)
+ self.checkInvalidParam(widget, name, '6x',
+ errmsg='bad screen distance "6x"', keep_orig=keep_orig)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='bad screen distance "spam"', keep_orig=keep_orig)
+
+ def checkReliefParam(self, widget, name):
+ self.checkParams(widget, name,
+ 'flat', 'groove', 'raised', 'ridge', 'solid', 'sunken')
+ errmsg='bad relief "spam": must be '\
+ 'flat, groove, raised, ridge, solid, or sunken'
+ if tcl_version < (8, 6):
+ errmsg = None
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg=errmsg)
+
+ def checkImageParam(self, widget, name):
+ image = tkinter.PhotoImage('image1')
+ self.checkParam(widget, name, image, conv=str)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='image "spam" doesn\'t exist')
+ widget[name] = ''
+
+ def checkVariableParam(self, widget, name, var):
+ self.checkParam(widget, name, var, conv=str)
+
+
+class StandardOptionsTests:
+ STANDARD_OPTIONS = (
+ 'activebackground', 'activeborderwidth', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
+ 'disabledforeground', 'exportselection', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'jump', 'justify', 'orient', 'padx', 'pady', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'setgrid', 'takefocus', 'text', 'textvariable', 'troughcolor',
+ 'underline', 'wraplength', 'xscrollcommand', 'yscrollcommand',
+ )
+
+ def test_activebackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'activebackground')
+
+ def test_activeborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'activeborderwidth',
+ 0, 1.3, 2.9, 6, -2, '10p')
+
+ def test_activeforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'activeforeground')
+
+ def test_anchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'anchor',
+ 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center')
+
+ def test_background(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'background')
+ if 'bg' in self.OPTIONS:
+ self.checkColorParam(widget, 'bg')
+
+ def test_bitmap(self):
+ widget = self.create()
+ self.checkParam(widget, 'bitmap', 'questhead')
+ self.checkParam(widget, 'bitmap', 'gray50')
+ self.checkInvalidParam(widget, 'bitmap', 'spam',
+ errmsg='bitmap "spam" not defined')
+
+ def test_borderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'borderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+ if 'bd' in self.OPTIONS:
+ self.checkPixelsParam(widget, 'bd', 0, 1.3, 2.6, 6, -2, '10p')
+
+ def test_compound(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'compound',
+ 'bottom', 'center', 'left', 'none', 'right', 'top')
+
+ def test_cursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'cursor')
+
+ def test_disabledforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'disabledforeground')
+
+ def test_exportselection(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'exportselection')
+
+ def test_font(self):
+ widget = self.create()
+ self.checkParam(widget, 'font',
+ '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
+ self.checkInvalidParam(widget, 'font', '',
+ errmsg='font "" doesn\'t exist')
+
+ def test_foreground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'foreground')
+ if 'fg' in self.OPTIONS:
+ self.checkColorParam(widget, 'fg')
+
+ def test_highlightbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'highlightbackground')
+
+ def test_highlightcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'highlightcolor')
+
+ def test_highlightthickness(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'highlightthickness',
+ 0, 1.3, 2.6, 6, '10p')
+ self.checkParam(widget, 'highlightthickness', -2, expected=0,
+ conv=self._conv_pixels)
+
+ def test_image(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'image')
+
+ def test_insertbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'insertbackground')
+
+ def test_insertborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertborderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+
+ def test_insertofftime(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'insertofftime', 100)
+
+ def test_insertontime(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'insertontime', 100)
+
+ def test_insertwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertwidth', 1.3, 2.6, -2, '10p')
+
+ def test_jump(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'jump')
+
+ def test_justify(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'justify', 'left', 'right', 'center',
+ errmsg='bad justification "{}": must be '
+ 'left, right, or center')
+ self.checkInvalidParam(widget, 'justify', '',
+ errmsg='ambiguous justification "": must be '
+ 'left, right, or center')
+
+ def test_orient(self):
+ widget = self.create()
+ self.assertEqual(str(widget['orient']), self.default_orient)
+ self.checkEnumParam(widget, 'orient', 'horizontal', 'vertical')
+
+ def test_padx(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, -2, '12m',
+ conv=self._conv_pad_pixels)
+
+ def test_pady(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, -2, '12m',
+ conv=self._conv_pad_pixels)
+
+ def test_relief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'relief')
+
+ def test_repeatdelay(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'repeatdelay', -500, 500)
+
+ def test_repeatinterval(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'repeatinterval', -500, 500)
+
+ def test_selectbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectbackground')
+
+ def test_selectborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'selectborderwidth', 1.3, 2.6, -2, '10p')
+
+ def test_selectforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectforeground')
+
+ def test_setgrid(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'setgrid')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'active', 'disabled', 'normal')
+
+ def test_takefocus(self):
+ widget = self.create()
+ self.checkParams(widget, 'takefocus', '0', '1', '')
+
+ def test_text(self):
+ widget = self.create()
+ self.checkParams(widget, 'text', '', 'any string')
+
+ def test_textvariable(self):
+ widget = self.create()
+ var = tkinter.StringVar()
+ self.checkVariableParam(widget, 'textvariable', var)
+
+ def test_troughcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'troughcolor')
+
+ def test_underline(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'underline', 0, 1, 10)
+
+ def test_wraplength(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkPixelsParam(widget, 'wraplength', 100)
+ else:
+ self.checkParams(widget, 'wraplength', 100)
+
+ def test_xscrollcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'xscrollcommand')
+
+ def test_yscrollcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'yscrollcommand')
+
+ # non-standard but common options
+
+ def test_command(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'command')
+
+ def test_indicatoron(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'indicatoron')
+
+ def test_offrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'offrelief')
+
+ def test_overrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'overrelief')
+
+ def test_selectcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectcolor')
+
+ def test_selectimage(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'selectimage')
+
+ @requires_tcl(8, 5)
+ def test_tristateimage(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'tristateimage')
+
+ @requires_tcl(8, 5)
+ def test_tristatevalue(self):
+ widget = self.create()
+ self.checkParam(widget, 'tristatevalue', 'unknowable')
+
+ def test_variable(self):
+ widget = self.create()
+ var = tkinter.DoubleVar()
+ self.checkVariableParam(widget, 'variable', var)
+
+
+class IntegerSizeTests:
+ def test_height(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'height', 100, -100, 0)
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402, -402, 0)
+
+
+class PixelSizeTests:
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '3c')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i')
+
+
+def add_standard_options(*source_classes):
+ # This decorator adds test_xxx methods from source classes for every xxx
+ # option in the OPTIONS class attribute if they are not defined explicitly.
+ def decorator(cls):
+ for option in cls.OPTIONS:
+ methodname = 'test_' + option
+ if not hasattr(cls, methodname):
+ for source_class in source_classes:
+ if hasattr(source_class, methodname):
+ setattr(cls, methodname,
+ getattr(source_class, methodname))
+ break
+ else:
+ def test(self, option=option):
+ widget = self.create()
+ widget[option]
+ raise AssertionError('Option "%s" is not tested in %s' %
+ (option, cls.__name__))
+ test.__name__ = methodname
+ setattr(cls, methodname, test)
+ return cls
+ return decorator
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -147,6 +147,8 @@
Tests
-----
+- Issue #19085: Added basic tests for all tkinter widget options.
+
- Issue 19384: Fix test_py_compile for root user, patch by Claudiu Popa.
Build
--
Repository URL: http://hg.python.org/cpython
1
0

cpython (3.3): Issue #19085: Added basic tests for all tkinter widget options.
by serhiy.storchaka Nov. 2, 2013
by serhiy.storchaka Nov. 2, 2013
Nov. 2, 2013
http://hg.python.org/cpython/rev/92e268f2719e
changeset: 86834:92e268f2719e
branch: 3.3
parent: 86814:731bdec35fdd
user: Serhiy Storchaka <storchaka(a)gmail.com>
date: Sat Nov 02 10:41:48 2013 +0200
summary:
Issue #19085: Added basic tests for all tkinter widget options.
files:
Lib/tkinter/test/support.py | 39 +
Lib/tkinter/test/test_tkinter/test_widgets.py | 919 ++++++++++
Lib/tkinter/test/test_ttk/test_widgets.py | 493 +++++-
Lib/tkinter/test/widget_tests.py | 487 +++++
Misc/NEWS | 5 +
5 files changed, 1920 insertions(+), 23 deletions(-)
diff --git a/Lib/tkinter/test/support.py b/Lib/tkinter/test/support.py
--- a/Lib/tkinter/test/support.py
+++ b/Lib/tkinter/test/support.py
@@ -77,3 +77,42 @@
widget.event_generate('<Motion>', x=x, y=y)
widget.event_generate('<ButtonPress-1>', x=x, y=y)
widget.event_generate('<ButtonRelease-1>', x=x, y=y)
+
+
+import _tkinter
+tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.')))
+
+def requires_tcl(*version):
+ return unittest.skipUnless(tcl_version >= version,
+ 'requires Tcl version >= ' + '.'.join(map(str, version)))
+
+units = {
+ 'c': 72 / 2.54, # centimeters
+ 'i': 72, # inches
+ 'm': 72 / 25.4, # millimeters
+ 'p': 1, # points
+}
+
+def pixels_conv(value):
+ return float(value[:-1]) * units[value[-1:]]
+
+def tcl_obj_eq(actual, expected):
+ if actual == expected:
+ return True
+ if isinstance(actual, _tkinter.Tcl_Obj):
+ if isinstance(expected, str):
+ return str(actual) == expected
+ if isinstance(actual, tuple):
+ if isinstance(expected, tuple):
+ return (len(actual) == len(expected) and
+ all(tcl_obj_eq(act, exp)
+ for act, exp in zip(actual, expected)))
+ return False
+
+def widget_eq(actual, expected):
+ if actual == expected:
+ return True
+ if isinstance(actual, (str, tkinter.Widget)):
+ if isinstance(expected, (str, tkinter.Widget)):
+ return str(actual) == str(expected)
+ return False
diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py
new file mode 100644
--- /dev/null
+++ b/Lib/tkinter/test/test_tkinter/test_widgets.py
@@ -0,0 +1,919 @@
+import unittest
+import tkinter
+import os
+from test.support import requires
+
+from tkinter.test.support import tcl_version, requires_tcl, widget_eq
+from tkinter.test.widget_tests import (add_standard_options, noconv,
+ AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests)
+
+requires('gui')
+
+
+def float_round(x):
+ return float(round(x))
+
+
+class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
+ _conv_pad_pixels = noconv
+
+ def test_class(self):
+ widget = self.create()
+ self.assertEqual(widget['class'],
+ widget.__class__.__name__.title())
+ self.checkInvalidParam(widget, 'class', 'Foo',
+ errmsg="can't modify -class option after widget is created")
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+
+ def test_colormap(self):
+ widget = self.create()
+ self.assertEqual(widget['colormap'], '')
+ self.checkInvalidParam(widget, 'colormap', 'new',
+ errmsg="can't modify -colormap option after widget is created")
+ widget2 = self.create(colormap='new')
+ self.assertEqual(widget2['colormap'], 'new')
+
+ def test_container(self):
+ widget = self.create()
+ self.assertEqual(widget['container'], 0 if self.wantobjects else '0')
+ self.checkInvalidParam(widget, 'container', 1,
+ errmsg="can't modify -container option after widget is created")
+ widget2 = self.create(container=True)
+ self.assertEqual(widget2['container'], 1 if self.wantobjects else '1')
+
+ def test_visual(self):
+ widget = self.create()
+ self.assertEqual(widget['visual'], '')
+ self.checkInvalidParam(widget, 'visual', 'default',
+ errmsg="can't modify -visual option after widget is created")
+ widget2 = self.create(visual='default')
+ self.assertEqual(widget2['visual'], 'default')
+
+
+@add_standard_options(StandardOptionsTests)
+class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'menu', 'padx', 'pady', 'relief', 'screen',
+ 'takefocus', 'use', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Toplevel(self.root, **kwargs)
+
+ def test_menu(self):
+ widget = self.create()
+ menu = tkinter.Menu(self.root)
+ self.checkParam(widget, 'menu', menu, eq=widget_eq)
+ self.checkParam(widget, 'menu', '')
+
+ def test_screen(self):
+ widget = self.create()
+ self.assertEqual(widget['screen'], '')
+ display = os.environ['DISPLAY']
+ self.checkInvalidParam(widget, 'screen', display,
+ errmsg="can't modify -screen option after widget is created")
+ widget2 = self.create(screen=display)
+ self.assertEqual(widget2['screen'], display)
+
+ def test_use(self):
+ widget = self.create()
+ self.assertEqual(widget['use'], '')
+ widget1 = self.create(container=True)
+ self.assertEqual(widget1['use'], '')
+ self.checkInvalidParam(widget1, 'use', '0x44022',
+ errmsg="can't modify -use option after widget is created")
+ wid = hex(widget1.winfo_id())
+ widget2 = self.create(use=wid)
+ self.assertEqual(widget2['use'], wid)
+
+
+@add_standard_options(StandardOptionsTests)
+class FrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'relief', 'takefocus', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Frame(self.root, **kwargs)
+
+
+@add_standard_options(StandardOptionsTests)
+class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'class', 'colormap', 'container', 'cursor',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'labelanchor', 'labelwidget', 'padx', 'pady', 'relief',
+ 'takefocus', 'text', 'visual', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.LabelFrame(self.root, **kwargs)
+
+ def test_labelanchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'labelanchor',
+ 'e', 'en', 'es', 'n', 'ne', 'nw',
+ 's', 'se', 'sw', 'w', 'wn', 'ws')
+ self.checkInvalidParam(widget, 'labelanchor', 'center')
+
+ def test_labelwidget(self):
+ widget = self.create()
+ label = tkinter.Label(self.root, text='Mupp', name='foo')
+ self.checkParam(widget, 'labelwidget', label, expected='.foo')
+ label.destroy()
+
+
+class AbstractLabelTest(AbstractWidgetTest, IntegerSizeTests):
+ _conv_pixels = noconv
+
+ def test_highlightthickness(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'highlightthickness',
+ 0, 1.3, 2.6, 6, -2, '10p')
+
+
+@add_standard_options(StandardOptionsTests)
+class LabelTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'justify', 'padx', 'pady', 'relief', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Label(self.root, **kwargs)
+
+
+@add_standard_options(StandardOptionsTests)
+class ButtonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor', 'default',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'state', 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength')
+
+ def _create(self, **kwargs):
+ return tkinter.Button(self.root, **kwargs)
+
+ def test_default(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'default', 'active', 'disabled', 'normal')
+
+
+@add_standard_options(StandardOptionsTests)
+class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify',
+ 'offrelief', 'offvalue', 'onvalue', 'overrelief',
+ 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'tristateimage', 'tristatevalue',
+ 'underline', 'variable', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Checkbutton(self.root, **kwargs)
+
+
+ def test_offvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
+
+ def test_onvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
+
+
+@add_standard_options(StandardOptionsTests)
+class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'command', 'compound', 'cursor',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify', 'offrelief', 'overrelief',
+ 'padx', 'pady', 'relief', 'selectcolor', 'selectimage', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'tristateimage', 'tristatevalue',
+ 'underline', 'value', 'variable', 'width', 'wraplength',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Radiobutton(self.root, **kwargs)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
+
+
+@add_standard_options(StandardOptionsTests)
+class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth',
+ 'compound', 'cursor', 'direction',
+ 'disabledforeground', 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'indicatoron', 'justify', 'menu',
+ 'padx', 'pady', 'relief', 'state',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+ _conv_pixels = AbstractWidgetTest._conv_pixels
+
+ def _create(self, **kwargs):
+ return tkinter.Menubutton(self.root, **kwargs)
+
+ def test_direction(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'direction',
+ 'above', 'below', 'flush', 'left', 'right')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'height', 100, -100, 0, conv=str)
+
+ test_highlightthickness = StandardOptionsTests.test_highlightthickness
+
+ def test_image(self):
+ widget = self.create()
+ image = tkinter.PhotoImage('image1')
+ self.checkParam(widget, 'image', image, conv=str)
+ errmsg = 'image "spam" doesn\'t exist'
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget['image'] = 'spam'
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget.configure({'image': 'spam'})
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+
+ def test_menu(self):
+ widget = self.create()
+ menu = tkinter.Menu(widget, name='menu')
+ self.checkParam(widget, 'menu', menu, eq=widget_eq)
+ menu.destroy()
+
+ def test_padx(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, '12m')
+ self.checkParam(widget, 'padx', -2, expected=0)
+
+ def test_pady(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, '12m')
+ self.checkParam(widget, 'pady', -2, expected=0)
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402, -402, 0, conv=str)
+
+
+class OptionMenuTest(MenubuttonTest, unittest.TestCase):
+
+ def _create(self, default='b', values=('a', 'b', 'c'), **kwargs):
+ return tkinter.OptionMenu(self.root, None, default, *values, **kwargs)
+
+
+@add_standard_options(IntegerSizeTests, StandardOptionsTests)
+class EntryTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth', 'cursor',
+ 'disabledbackground', 'disabledforeground',
+ 'exportselection', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'invalidcommand', 'justify', 'readonlybackground', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'show', 'state', 'takefocus', 'textvariable',
+ 'validate', 'validatecommand', 'width', 'xscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Entry(self.root, **kwargs)
+
+ def test_disabledbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'disabledbackground')
+
+ def test_insertborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertborderwidth', 0, 1.3, -2)
+ self.checkParam(widget, 'insertborderwidth', 2, expected=1)
+ self.checkParam(widget, 'insertborderwidth', '10p', expected=1)
+
+ def test_insertwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertwidth', 1.3, 3.6, '10p')
+ if tcl_version[:2] == (8, 5):
+ self.checkParam(widget, 'insertwidth', 0.9, expected=2)
+ else:
+ self.checkParam(widget, 'insertwidth', 0.9, expected=1)
+ self.checkParam(widget, 'insertwidth', 0.1, expected=2)
+ self.checkParam(widget, 'insertwidth', -2, expected=2)
+
+ def test_invalidcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'invalidcommand')
+ self.checkCommandParam(widget, 'invcmd')
+
+ def test_readonlybackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'readonlybackground')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', '*')
+ self.checkParam(widget, 'show', '')
+ self.checkParam(widget, 'show', ' ')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state',
+ 'disabled', 'normal', 'readonly')
+
+ def test_validate(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'validate',
+ 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
+
+ def test_validatecommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'validatecommand')
+ self.checkCommandParam(widget, 'vcmd')
+
+
+@add_standard_options(StandardOptionsTests)
+class SpinboxTest(EntryTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'background', 'borderwidth',
+ 'buttonbackground', 'buttoncursor', 'buttondownrelief', 'buttonuprelief',
+ 'command', 'cursor', 'disabledbackground', 'disabledforeground',
+ 'exportselection', 'font', 'foreground', 'format', 'from',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'increment',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'invalidcommand', 'justify', 'relief', 'readonlybackground',
+ 'repeatdelay', 'repeatinterval',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'state', 'takefocus', 'textvariable', 'to',
+ 'validate', 'validatecommand', 'values',
+ 'width', 'wrap', 'xscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Spinbox(self.root, **kwargs)
+
+ test_show = None
+
+ def test_buttonbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'buttonbackground')
+
+ def test_buttoncursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'buttoncursor')
+
+ def test_buttondownrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'buttondownrelief')
+
+ def test_buttonuprelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'buttonuprelief')
+
+ def test_format(self):
+ widget = self.create()
+ self.checkParam(widget, 'format', '%2f')
+ self.checkParam(widget, 'format', '%2.2f')
+ self.checkParam(widget, 'format', '%.2f')
+ self.checkParam(widget, 'format', '%2.f')
+ self.checkInvalidParam(widget, 'format', '%2e-1f')
+ self.checkInvalidParam(widget, 'format', '2.2')
+ self.checkInvalidParam(widget, 'format', '%2.-2f')
+ self.checkParam(widget, 'format', '%-2.02f')
+ self.checkParam(widget, 'format', '% 2.02f')
+ self.checkParam(widget, 'format', '% -2.200f')
+ self.checkParam(widget, 'format', '%09.200f')
+ self.checkInvalidParam(widget, 'format', '%d')
+
+ def test_from(self):
+ widget = self.create()
+ self.checkParam(widget, 'to', 100.0)
+ self.checkFloatParam(widget, 'from', -10, 10.2, 11.7)
+ self.checkInvalidParam(widget, 'from', 200,
+ errmsg='-to value must be greater than -from value')
+
+ def test_increment(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'increment', -1, 1, 10.2, 12.8, 0)
+
+ def test_to(self):
+ widget = self.create()
+ self.checkParam(widget, 'from', -100.0)
+ self.checkFloatParam(widget, 'to', -10, 10.2, 11.7)
+ self.checkInvalidParam(widget, 'to', -200,
+ errmsg='-to value must be greater than -from value')
+
+ def test_values(self):
+ # XXX
+ widget = self.create()
+ self.assertEqual(widget['values'], '')
+ self.checkParam(widget, 'values', 'mon tue wed thur')
+ self.checkParam(widget, 'values', ('mon', 'tue', 'wed', 'thur'),
+ expected='mon tue wed thur')
+ self.checkParam(widget, 'values', (42, 3.14, '', 'any string'),
+ expected='42 3.14 {} {any string}')
+ self.checkParam(widget, 'values', '')
+
+ def test_wrap(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'wrap')
+
+
+@add_standard_options(StandardOptionsTests)
+class TextTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'autoseparators', 'background', 'blockcursor', 'borderwidth',
+ 'cursor', 'endline', 'exportselection',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'inactiveselectbackground', 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertunfocussed', 'insertwidth',
+ 'maxundo', 'padx', 'pady', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'setgrid', 'spacing1', 'spacing2', 'spacing3', 'startline', 'state',
+ 'tabs', 'tabstyle', 'takefocus', 'undo', 'width', 'wrap',
+ 'xscrollcommand', 'yscrollcommand',
+ )
+ if tcl_version < (8, 5):
+ wantobjects = False
+
+ def _create(self, **kwargs):
+ return tkinter.Text(self.root, **kwargs)
+
+ def test_autoseparators(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'autoseparators')
+
+ @requires_tcl(8, 5)
+ def test_blockcursor(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'blockcursor')
+
+ @requires_tcl(8, 5)
+ def test_endline(self):
+ widget = self.create()
+ text = '\n'.join('Line %d' for i in range(100))
+ widget.insert('end', text)
+ self.checkParam(widget, 'endline', 200, expected='')
+ self.checkParam(widget, 'endline', -10, expected='')
+ self.checkInvalidParam(widget, 'endline', 'spam',
+ errmsg='expected integer but got "spam"')
+ self.checkParam(widget, 'endline', 50)
+ self.checkParam(widget, 'startline', 15)
+ self.checkInvalidParam(widget, 'endline', 10,
+ errmsg='-startline must be less than or equal to -endline')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, '3c')
+ self.checkParam(widget, 'height', -100, expected=1)
+ self.checkParam(widget, 'height', 0, expected=1)
+
+ def test_maxundo(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'maxundo', 0, 5, -1)
+
+ @requires_tcl(8, 5)
+ def test_inactiveselectbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'inactiveselectbackground')
+
+ @requires_tcl(8, 6)
+ def test_insertunfocussed(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'insertunfocussed',
+ 'hollow', 'none', 'solid')
+
+ def test_selectborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'selectborderwidth',
+ 1.3, 2.6, -2, '10p', conv=False,
+ keep_orig=tcl_version >= (8, 5))
+
+ def test_spacing1(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing1', 20, 21.4, 22.6, '0.5c')
+ self.checkParam(widget, 'spacing1', -5, expected=0)
+
+ def test_spacing2(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing2', 5, 6.4, 7.6, '0.1c')
+ self.checkParam(widget, 'spacing2', -1, expected=0)
+
+ def test_spacing3(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'spacing3', 20, 21.4, 22.6, '0.5c')
+ self.checkParam(widget, 'spacing3', -10, expected=0)
+
+ @requires_tcl(8, 5)
+ def test_startline(self):
+ widget = self.create()
+ text = '\n'.join('Line %d' for i in range(100))
+ widget.insert('end', text)
+ self.checkParam(widget, 'startline', 200, expected='')
+ self.checkParam(widget, 'startline', -10, expected='')
+ self.checkInvalidParam(widget, 'startline', 'spam',
+ errmsg='expected integer but got "spam"')
+ self.checkParam(widget, 'startline', 10)
+ self.checkParam(widget, 'endline', 50)
+ self.checkInvalidParam(widget, 'startline', 70,
+ errmsg='-startline must be less than or equal to -endline')
+
+ def test_state(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkParams(widget, 'state', 'disabled', 'normal')
+ else:
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal')
+
+ def test_tabs(self):
+ widget = self.create()
+ self.checkParam(widget, 'tabs', (10.2, 20.7, '1i', '2i'))
+ self.checkParam(widget, 'tabs', '10.2 20.7 1i 2i',
+ expected=('10.2', '20.7', '1i', '2i'))
+ self.checkParam(widget, 'tabs', '2c left 4c 6c center',
+ expected=('2c', 'left', '4c', '6c', 'center'))
+ self.checkInvalidParam(widget, 'tabs', 'spam',
+ errmsg='bad screen distance "spam"',
+ keep_orig=tcl_version >= (8, 5))
+
+ @requires_tcl(8, 5)
+ def test_tabstyle(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'tabstyle', 'tabular', 'wordprocessor')
+
+ def test_undo(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'undo')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402)
+ self.checkParam(widget, 'width', -402, expected=1)
+ self.checkParam(widget, 'width', 0, expected=1)
+
+ def test_wrap(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkParams(widget, 'wrap', 'char', 'none', 'word')
+ else:
+ self.checkEnumParam(widget, 'wrap', 'char', 'none', 'word')
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class CanvasTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth',
+ 'closeenough', 'confine', 'cursor', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'relief', 'scrollregion',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'state', 'takefocus',
+ 'xscrollcommand', 'xscrollincrement',
+ 'yscrollcommand', 'yscrollincrement', 'width',
+ )
+
+ _conv_pixels = round
+ wantobjects = False
+
+ def _create(self, **kwargs):
+ return tkinter.Canvas(self.root, **kwargs)
+
+ def test_closeenough(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'closeenough', 24, 2.4, 3.6, -3,
+ conv=float)
+
+ def test_confine(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'confine')
+
+ def test_scrollregion(self):
+ widget = self.create()
+ self.checkParam(widget, 'scrollregion', '0 0 200 150')
+ self.checkParam(widget, 'scrollregion', (0, 0, 200, 150),
+ expected='0 0 200 150')
+ self.checkParam(widget, 'scrollregion', '')
+ self.checkInvalidParam(widget, 'scrollregion', 'spam',
+ errmsg='bad scrollRegion "spam"')
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 'spam'))
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200))
+ self.checkInvalidParam(widget, 'scrollregion', (0, 0, 200, 150, 0))
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal',
+ errmsg='bad state value "{}": must be normal or disabled')
+
+ def test_xscrollincrement(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'xscrollincrement',
+ 40, 0, 41.2, 43.6, -40, '0.5i')
+
+ def test_yscrollincrement(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'yscrollincrement',
+ 10, 0, 11.2, 13.6, -10, '0.1i')
+
+
+@add_standard_options(IntegerSizeTests, StandardOptionsTests)
+class ListboxTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activestyle', 'background', 'borderwidth', 'cursor',
+ 'disabledforeground', 'exportselection',
+ 'font', 'foreground', 'height',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'listvariable', 'relief',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'selectmode', 'setgrid', 'state',
+ 'takefocus', 'width', 'xscrollcommand', 'yscrollcommand',
+ )
+
+ def _create(self, **kwargs):
+ return tkinter.Listbox(self.root, **kwargs)
+
+ def test_activestyle(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'activestyle',
+ 'dotbox', 'none', 'underline')
+
+ def test_listvariable(self):
+ widget = self.create()
+ var = tkinter.DoubleVar()
+ self.checkVariableParam(widget, 'listvariable', var)
+
+ def test_selectmode(self):
+ widget = self.create()
+ self.checkParam(widget, 'selectmode', 'single')
+ self.checkParam(widget, 'selectmode', 'browse')
+ self.checkParam(widget, 'selectmode', 'multiple')
+ self.checkParam(widget, 'selectmode', 'extended')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'disabled', 'normal')
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class ScaleTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'background', 'bigincrement', 'borderwidth',
+ 'command', 'cursor', 'digits', 'font', 'foreground', 'from',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'label', 'length', 'orient', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'resolution', 'showvalue', 'sliderlength', 'sliderrelief', 'state',
+ 'takefocus', 'tickinterval', 'to', 'troughcolor', 'variable', 'width',
+ )
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return tkinter.Scale(self.root, **kwargs)
+
+ def test_bigincrement(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'bigincrement', 12.4, 23.6, -5)
+
+ def test_digits(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'digits', 5, 0)
+
+ def test_from(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=float_round)
+
+ def test_label(self):
+ widget = self.create()
+ self.checkParam(widget, 'label', 'any string')
+ self.checkParam(widget, 'label', '')
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
+
+ def test_resolution(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'resolution', 4.2, 0, 6.7, -2)
+
+ def test_showvalue(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'showvalue')
+
+ def test_sliderlength(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sliderlength',
+ 10, 11.2, 15.6, -3, '3m')
+
+ def test_sliderrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'sliderrelief')
+
+ def test_tickinterval(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'tickinterval', 1, 4.3, 7.6, 0,
+ conv=float_round)
+ self.checkParam(widget, 'tickinterval', -2, expected=2,
+ conv=float_round)
+
+ def test_to(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10,
+ conv=float_round)
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activerelief',
+ 'background', 'borderwidth',
+ 'command', 'cursor', 'elementborderwidth',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'jump', 'orient', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'takefocus', 'troughcolor', 'width',
+ )
+ _conv_pixels = round
+ wantobjects = False
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return tkinter.Scrollbar(self.root, **kwargs)
+
+ def test_activerelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'activerelief')
+
+ def test_elementborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'elementborderwidth', 4.3, 5.6, -2, '1m')
+
+ def test_orient(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'orient', 'vertical', 'horizontal',
+ errmsg='bad orientation "{}": must be vertical or horizontal')
+
+
+@add_standard_options(StandardOptionsTests)
+class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'borderwidth', 'cursor',
+ 'handlepad', 'handlesize', 'height',
+ 'opaqueresize', 'orient', 'relief',
+ 'sashcursor', 'sashpad', 'sashrelief', 'sashwidth',
+ 'showhandle', 'width',
+ )
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return tkinter.PanedWindow(self.root, **kwargs)
+
+ def test_handlepad(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'handlepad', 5, 6.4, 7.6, -3, '1m')
+
+ def test_handlesize(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'handlesize', 8, 9.4, 10.6, -3, '2m',
+ conv=noconv)
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i',
+ conv=noconv)
+
+ def test_opaqueresize(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'opaqueresize')
+
+ def test_sashcursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'sashcursor')
+
+ def test_sashpad(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sashpad', 8, 1.3, 2.6, -2, '2m')
+
+ def test_sashrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'sashrelief')
+
+ def test_sashwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'sashwidth', 10, 11.1, 15.6, -3, '1m',
+ conv=noconv)
+
+ def test_showhandle(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'showhandle')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i',
+ conv=noconv)
+
+
+@add_standard_options(StandardOptionsTests)
+class MenuTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'activebackground', 'activeborderwidth', 'activeforeground',
+ 'background', 'borderwidth', 'cursor',
+ 'disabledforeground', 'font', 'foreground',
+ 'postcommand', 'relief', 'selectcolor', 'takefocus',
+ 'tearoff', 'tearoffcommand', 'title', 'type',
+ )
+ _conv_pixels = noconv
+
+ def _create(self, **kwargs):
+ return tkinter.Menu(self.root, **kwargs)
+
+ def test_postcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'postcommand')
+
+ def test_tearoff(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'tearoff')
+
+ def test_tearoffcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'tearoffcommand')
+
+ def test_title(self):
+ widget = self.create()
+ self.checkParam(widget, 'title', 'any string')
+
+ def test_type(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'type',
+ 'normal', 'tearoff', 'menubar')
+
+
+@add_standard_options(PixelSizeTests, StandardOptionsTests)
+class MessageTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'anchor', 'aspect', 'background', 'borderwidth',
+ 'cursor', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'justify', 'padx', 'pady', 'relief',
+ 'takefocus', 'text', 'textvariable', 'width',
+ )
+ _conv_pad_pixels = noconv
+
+ def _create(self, **kwargs):
+ return tkinter.Message(self.root, **kwargs)
+
+ def test_aspect(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'aspect', 250, 0, -300)
+
+
+tests_gui = (
+ ButtonTest, CanvasTest, CheckbuttonTest, EntryTest,
+ FrameTest, LabelFrameTest,LabelTest, ListboxTest,
+ MenubuttonTest, MenuTest, MessageTest, OptionMenuTest,
+ PanedWindowTest, RadiobuttonTest, ScaleTest, ScrollbarTest,
+ SpinboxTest, TextTest, ToplevelTest,
+)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/Lib/tkinter/test/test_ttk/test_widgets.py b/Lib/tkinter/test/test_ttk/test_widgets.py
--- a/Lib/tkinter/test/test_ttk/test_widgets.py
+++ b/Lib/tkinter/test/test_ttk/test_widgets.py
@@ -1,15 +1,57 @@
import unittest
import tkinter
-import os
from tkinter import ttk
-from test.support import requires, run_unittest
+from test.support import requires
import sys
import tkinter.test.support as support
-from tkinter.test.test_ttk.test_functions import MockTclObj, MockStateSpec
+from tkinter.test.test_ttk.test_functions import MockTclObj
+from tkinter.test.support import tcl_version
+from tkinter.test.widget_tests import (add_standard_options, noconv,
+ AbstractWidgetTest, StandardOptionsTests, IntegerSizeTests, PixelSizeTests)
requires('gui')
+
+class StandardTtkOptionsTests(StandardOptionsTests):
+
+ def test_class(self):
+ widget = self.create()
+ self.assertEqual(widget['class'], '')
+ errmsg='attempt to change read-only option'
+ if tcl_version < (8, 6):
+ errmsg='Attempt to change read-only option'
+ self.checkInvalidParam(widget, 'class', 'Foo', errmsg=errmsg)
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+
+ def test_padding(self):
+ widget = self.create()
+ self.checkParam(widget, 'padding', 0, expected=('0',))
+ self.checkParam(widget, 'padding', 5, expected=('5',))
+ self.checkParam(widget, 'padding', (5, 6), expected=('5', '6'))
+ self.checkParam(widget, 'padding', (5, 6, 7),
+ expected=('5', '6', '7'))
+ self.checkParam(widget, 'padding', (5, 6, 7, 8),
+ expected=('5', '6', '7', '8'))
+ self.checkParam(widget, 'padding', ('5p', '6p', '7p', '8p'))
+ self.checkParam(widget, 'padding', (), expected='')
+
+ def test_style(self):
+ widget = self.create()
+ self.assertEqual(widget['style'], '')
+ errmsg = 'Layout Foo not found'
+ if hasattr(self, 'default_orient'):
+ errmsg = ('Layout %s.Foo not found' %
+ getattr(self, 'default_orient').title())
+ self.checkInvalidParam(widget, 'style', 'Foo',
+ errmsg=errmsg)
+ widget2 = self.create(class_='Foo')
+ self.assertEqual(widget2['class'], 'Foo')
+ # XXX
+ pass
+
+
class WidgetTest(unittest.TestCase):
"""Tests methods available in every ttk widget."""
@@ -73,7 +115,112 @@
self.assertEqual(self.widget.state(), ('active', ))
-class ButtonTest(unittest.TestCase):
+class AbstractToplevelTest(AbstractWidgetTest, PixelSizeTests):
+ _conv_pixels = noconv
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class FrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'borderwidth', 'class', 'cursor', 'height',
+ 'padding', 'relief', 'style', 'takefocus',
+ 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Frame(self.root, **kwargs)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
+ OPTIONS = (
+ 'borderwidth', 'class', 'cursor', 'height',
+ 'labelanchor', 'labelwidget',
+ 'padding', 'relief', 'style', 'takefocus',
+ 'text', 'underline', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.LabelFrame(self.root, **kwargs)
+
+ def test_labelanchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'labelanchor',
+ 'e', 'en', 'es', 'n', 'ne', 'nw', 's', 'se', 'sw', 'w', 'wn', 'ws',
+ errmsg='Bad label anchor specification {}')
+ self.checkInvalidParam(widget, 'labelanchor', 'center')
+
+ def test_labelwidget(self):
+ widget = self.create()
+ label = ttk.Label(self.root, text='Mupp', name='foo')
+ self.checkParam(widget, 'labelwidget', label, expected='.foo')
+ label.destroy()
+
+
+class AbstractLabelTest(AbstractWidgetTest):
+
+ def checkImageParam(self, widget, name):
+ image = tkinter.PhotoImage('image1')
+ image2 = tkinter.PhotoImage('image2')
+ self.checkParam(widget, name, image, expected=('image1',))
+ self.checkParam(widget, name, 'image1', expected=('image1',))
+ self.checkParam(widget, name, (image,), expected=('image1',))
+ self.checkParam(widget, name, (image, 'active', image2),
+ expected=('image1', 'active', 'image2'))
+ self.checkParam(widget, name, 'image1 active image2',
+ expected=('image1', 'active', 'image2'))
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='image "spam" doesn\'t exist')
+
+ def test_compound(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'compound',
+ 'none', 'text', 'image', 'center',
+ 'top', 'bottom', 'left', 'right')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkParams(widget, 'width', 402, -402, 0)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class LabelTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'anchor', 'background',
+ 'class', 'compound', 'cursor', 'font', 'foreground',
+ 'image', 'justify', 'padding', 'relief', 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width', 'wraplength',
+ )
+ _conv_pixels = noconv
+
+ def _create(self, **kwargs):
+ return ttk.Label(self.root, **kwargs)
+
+ def test_font(self):
+ widget = self.create()
+ self.checkParam(widget, 'font',
+ '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class ButtonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor', 'default',
+ 'image', 'state', 'style', 'takefocus', 'text', 'textvariable',
+ 'underline', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Button(self.root, **kwargs)
+
+ def test_default(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'default', 'normal', 'active', 'disabled')
def test_invoke(self):
success = []
@@ -82,7 +229,27 @@
self.assertTrue(success)
-class CheckbuttonTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor',
+ 'image',
+ 'offvalue', 'onvalue',
+ 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'variable', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Checkbutton(self.root, **kwargs)
+
+ def test_offvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'offvalue', 1, 2.3, '', 'any string')
+
+ def test_onvalue(self):
+ widget = self.create()
+ self.checkParams(widget, 'onvalue', 1, 2.3, '', 'any string')
def test_invoke(self):
success = []
@@ -105,21 +272,40 @@
cbtn['command'] = ''
res = cbtn.invoke()
- self.assertEqual(str(res), '')
+ self.assertFalse(str(res))
self.assertFalse(len(success) > 1)
self.assertEqual(cbtn['offvalue'],
cbtn.tk.globalgetvar(cbtn['variable']))
-class ComboboxTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class ComboboxTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'exportselection', 'height',
+ 'justify', 'postcommand', 'state', 'style',
+ 'takefocus', 'textvariable', 'values', 'width',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.combo = ttk.Combobox()
+ self.combo = self.create()
def tearDown(self):
self.combo.destroy()
support.root_withdraw()
+ super().tearDown()
+
+ def _create(self, **kwargs):
+ return ttk.Combobox(self.root, **kwargs)
+
+ def test_height(self):
+ widget = self.create()
+ self.checkParams(widget, 'height', 100, 101.2, 102.6, -100, 0, '1i')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state', 'active', 'disabled', 'normal')
def _show_drop_down_listbox(self):
width = self.combo.winfo_width()
@@ -167,8 +353,16 @@
self.assertEqual(self.combo.get(), getval)
self.assertEqual(self.combo.current(), currval)
+ self.assertEqual(self.combo['values'],
+ () if tcl_version < (8, 5) else '')
check_get_current('', -1)
+ self.checkParam(self.combo, 'values', 'mon tue wed thur',
+ expected=('mon', 'tue', 'wed', 'thur'))
+ self.checkParam(self.combo, 'values', ('mon', 'tue', 'wed', 'thur'))
+ self.checkParam(self.combo, 'values', (42, 3.14, '', 'any string'))
+ self.checkParam(self.combo, 'values', '', expected=())
+
self.combo['values'] = ['a', 1, 'c']
self.combo.set('c')
@@ -209,15 +403,52 @@
combo2.destroy()
-class EntryTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class EntryTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'background', 'class', 'cursor',
+ 'exportselection', 'font',
+ 'invalidcommand', 'justify',
+ 'show', 'state', 'style', 'takefocus', 'textvariable',
+ 'validate', 'validatecommand', 'width', 'xscrollcommand',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.entry = ttk.Entry()
+ self.entry = self.create()
def tearDown(self):
self.entry.destroy()
support.root_withdraw()
+ super().tearDown()
+
+ def _create(self, **kwargs):
+ return ttk.Entry(self.root, **kwargs)
+
+ def test_invalidcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'invalidcommand')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', '*')
+ self.checkParam(widget, 'show', '')
+ self.checkParam(widget, 'show', ' ')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkParams(widget, 'state',
+ 'disabled', 'normal', 'readonly')
+
+ def test_validate(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'validate',
+ 'all', 'key', 'focus', 'focusin', 'focusout', 'none')
+
+ def test_validatecommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'validatecommand')
def test_bbox(self):
@@ -313,16 +544,36 @@
self.assertEqual(self.entry.state(), ())
-class PanedwindowTest(unittest.TestCase):
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'height',
+ 'orient', 'style', 'takefocus', 'width',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.paned = ttk.Panedwindow()
+ self.paned = self.create()
def tearDown(self):
self.paned.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.PanedWindow(self.root, **kwargs)
+
+ def test_orient(self):
+ widget = self.create()
+ self.assertEqual(str(widget['orient']), 'vertical')
+ errmsg='attempt to change read-only option'
+ if tcl_version < (8, 6):
+ errmsg='Attempt to change read-only option'
+ self.checkInvalidParam(widget, 'orient', 'horizontal',
+ errmsg=errmsg)
+ widget2 = self.create(orient='horizontal')
+ self.assertEqual(str(widget2['orient']), 'horizontal')
def test_add(self):
# attempt to add a child that is not a direct child of the paned window
@@ -432,7 +683,22 @@
self.assertTrue(isinstance(self.paned.sashpos(0), int))
-class RadiobuttonTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'compound', 'cursor',
+ 'image',
+ 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'value', 'variable', 'width',
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Radiobutton(self.root, **kwargs)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkParams(widget, 'value', 1, 2.3, '', 'any string')
def test_invoke(self):
success = []
@@ -462,19 +728,68 @@
self.assertEqual(str(cbtn['variable']), str(cbtn2['variable']))
+class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'compound', 'cursor', 'direction',
+ 'image', 'menu', 'state', 'style',
+ 'takefocus', 'text', 'textvariable',
+ 'underline', 'width',
+ )
-class ScaleTest(unittest.TestCase):
+ def _create(self, **kwargs):
+ return ttk.Menubutton(self.root, **kwargs)
+
+ def test_direction(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'direction',
+ 'above', 'below', 'left', 'right', 'flush')
+
+ def test_menu(self):
+ widget = self.create()
+ menu = tkinter.Menu(widget, name='menu')
+ self.checkParam(widget, 'menu', menu, conv=str)
+ menu.destroy()
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class ScaleTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'cursor', 'from', 'length',
+ 'orient', 'style', 'takefocus', 'to', 'value', 'variable',
+ )
+ _conv_pixels = noconv
+ default_orient = 'horizontal'
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.scale = ttk.Scale()
+ self.scale = self.create()
self.scale.pack()
self.scale.update()
def tearDown(self):
self.scale.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.Scale(self.root, **kwargs)
+
+ def test_from(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'from', 100, 14.9, 15.1, conv=False)
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 130, 131.2, 135.6, '5i')
+
+ def test_to(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'to', 300, 14.9, 15.1, -10, conv=False)
+
+ def test_value(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'value', 300, 14.9, 15.1, -10, conv=False)
def test_custom_event(self):
failure = [1, 1, 1] # will need to be empty
@@ -539,11 +854,64 @@
self.assertRaises(tkinter.TclError, self.scale.set, None)
-class NotebookTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class ProgressbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'orient', 'length',
+ 'mode', 'maximum', 'phase',
+ 'style', 'takefocus', 'value', 'variable',
+ )
+ _conv_pixels = noconv
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return ttk.Progressbar(self.root, **kwargs)
+
+ def test_length(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'length', 100.1, 56.7, '2i')
+
+ def test_maximum(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'maximum', 150.2, 77.7, 0, -10, conv=False)
+
+ def test_mode(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'mode', 'determinate', 'indeterminate')
+
+ def test_phase(self):
+ # XXX
+ pass
+
+ def test_value(self):
+ widget = self.create()
+ self.checkFloatParam(widget, 'value', 150.2, 77.7, 0, -10,
+ conv=False)
+
+
+(a)unittest.skipIf(sys.platform == 'darwin',
+ 'ttk.Scrollbar is special on MacOSX')
+@add_standard_options(StandardTtkOptionsTests)
+class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'command', 'cursor', 'orient', 'style', 'takefocus',
+ )
+ default_orient = 'vertical'
+
+ def _create(self, **kwargs):
+ return ttk.Scrollbar(self.root, **kwargs)
+
+
+@add_standard_options(IntegerSizeTests, StandardTtkOptionsTests)
+class NotebookTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'height', 'padding', 'style', 'takefocus',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.nb = ttk.Notebook(padding=0)
+ self.nb = self.create(padding=0)
self.child1 = ttk.Label()
self.child2 = ttk.Label()
self.nb.add(self.child1, text='a')
@@ -554,7 +922,10 @@
self.child2.destroy()
self.nb.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.Notebook(self.root, **kwargs)
def test_tab_identifiers(self):
self.nb.forget(0)
@@ -746,16 +1117,68 @@
self.assertEqual(self.nb.select(), str(self.child1))
-class TreeviewTest(unittest.TestCase):
+@add_standard_options(StandardTtkOptionsTests)
+class TreeviewTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'columns', 'cursor', 'displaycolumns',
+ 'height', 'padding', 'selectmode', 'show',
+ 'style', 'takefocus', 'xscrollcommand', 'yscrollcommand',
+ )
def setUp(self):
+ super().setUp()
support.root_deiconify()
- self.tv = ttk.Treeview(padding=0)
+ self.tv = self.create(padding=0)
def tearDown(self):
self.tv.destroy()
support.root_withdraw()
+ super().tearDown()
+ def _create(self, **kwargs):
+ return ttk.Treeview(self.root, **kwargs)
+
+ def test_columns(self):
+ widget = self.create()
+ self.checkParam(widget, 'columns', 'a b c',
+ expected=('a', 'b', 'c'))
+ self.checkParam(widget, 'columns', ('a', 'b', 'c'))
+ self.checkParam(widget, 'columns', ())
+
+ def test_displaycolumns(self):
+ widget = self.create()
+ widget['columns'] = ('a', 'b', 'c')
+ self.checkParam(widget, 'displaycolumns', 'b a c',
+ expected=('b', 'a', 'c'))
+ self.checkParam(widget, 'displaycolumns', ('b', 'a', 'c'))
+ self.checkParam(widget, 'displaycolumns', '#all',
+ expected=('#all',))
+ self.checkParam(widget, 'displaycolumns', (2, 1, 0))
+ self.checkInvalidParam(widget, 'displaycolumns', ('a', 'b', 'd'),
+ errmsg='Invalid column index d')
+ self.checkInvalidParam(widget, 'displaycolumns', (1, 2, 3),
+ errmsg='Column index 3 out of bounds')
+ self.checkInvalidParam(widget, 'displaycolumns', (1, -2),
+ errmsg='Column index -2 out of bounds')
+
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, -100, 0, '3c', conv=False)
+ self.checkPixelsParam(widget, 'height', 101.2, 102.6, conv=noconv)
+
+ def test_selectmode(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'selectmode',
+ 'none', 'browse', 'extended')
+
+ def test_show(self):
+ widget = self.create()
+ self.checkParam(widget, 'show', 'tree headings',
+ expected=('tree', 'headings'))
+ self.checkParam(widget, 'show', ('tree', 'headings'))
+ self.checkParam(widget, 'show', ('headings', 'tree'))
+ self.checkParam(widget, 'show', 'tree', expected=('tree',))
+ self.checkParam(widget, 'show', 'headings', expected=('headings',))
def test_bbox(self):
self.tv.pack()
@@ -1149,11 +1572,35 @@
self.assertTrue(isinstance(self.tv.tag_configure('test'), dict))
+@add_standard_options(StandardTtkOptionsTests)
+class SeparatorTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'orient', 'style', 'takefocus',
+ # 'state'?
+ )
+ default_orient = 'horizontal'
+
+ def _create(self, **kwargs):
+ return ttk.Separator(self.root, **kwargs)
+
+
+@add_standard_options(StandardTtkOptionsTests)
+class SizegripTest(AbstractWidgetTest, unittest.TestCase):
+ OPTIONS = (
+ 'class', 'cursor', 'style', 'takefocus',
+ # 'state'?
+ )
+
+ def _create(self, **kwargs):
+ return ttk.Sizegrip(self.root, **kwargs)
+
tests_gui = (
- WidgetTest, ButtonTest, CheckbuttonTest, RadiobuttonTest,
- ComboboxTest, EntryTest, PanedwindowTest, ScaleTest, NotebookTest,
- TreeviewTest
+ ButtonTest, CheckbuttonTest, ComboboxTest, EntryTest,
+ FrameTest, LabelFrameTest, LabelTest, MenubuttonTest,
+ NotebookTest, PanedWindowTest, ProgressbarTest,
+ RadiobuttonTest, ScaleTest, ScrollbarTest, SeparatorTest,
+ SizegripTest, TreeviewTest, WidgetTest,
)
if __name__ == "__main__":
- run_unittest(*tests_gui)
+ unittest.main()
diff --git a/Lib/tkinter/test/widget_tests.py b/Lib/tkinter/test/widget_tests.py
new file mode 100644
--- /dev/null
+++ b/Lib/tkinter/test/widget_tests.py
@@ -0,0 +1,487 @@
+# Common tests for test_tkinter/test_widgets.py and test_ttk/test_widgets.py
+
+import tkinter
+from tkinter.ttk import setup_master, Scale
+from tkinter.test.support import (tcl_version, requires_tcl, pixels_conv,
+ tcl_obj_eq)
+
+
+noconv = str if tcl_version < (8, 5) else False
+
+_sentinel = object()
+
+class AbstractWidgetTest:
+ _conv_pixels = round if tcl_version[:2] != (8, 5) else int
+ _conv_pad_pixels = None
+ wantobjects = True
+
+ def setUp(self):
+ self.root = setup_master()
+ self.scaling = float(self.root.call('tk', 'scaling'))
+ if not self.root.wantobjects():
+ self.wantobjects = False
+
+ def create(self, **kwargs):
+ widget = self._create(**kwargs)
+ self.addCleanup(widget.destroy)
+ return widget
+
+ def assertEqual2(self, actual, expected, msg=None, eq=object.__eq__):
+ if eq(actual, expected):
+ return
+ self.assertEqual(actual, expected, msg)
+
+ def checkParam(self, widget, name, value, *, expected=_sentinel,
+ conv=False, eq=None):
+ widget[name] = value
+ if expected is _sentinel:
+ expected = value
+ if conv:
+ expected = conv(expected)
+ if not self.wantobjects:
+ if isinstance(expected, tuple):
+ expected = tkinter._join(expected)
+ else:
+ expected = str(expected)
+ if eq is None:
+ eq = tcl_obj_eq
+ self.assertEqual2(widget[name], expected, eq=eq)
+ self.assertEqual2(widget.cget(name), expected, eq=eq)
+ # XXX
+ if not isinstance(widget, Scale):
+ t = widget.configure(name)
+ self.assertEqual(len(t), 5)
+ ## XXX
+ if not isinstance(t[4], tuple):
+ self.assertEqual2(t[4], expected, eq=eq)
+
+ def checkInvalidParam(self, widget, name, value, errmsg=None, *,
+ keep_orig=True):
+ orig = widget[name]
+ if errmsg is not None:
+ errmsg = errmsg.format(value)
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget[name] = value
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ if keep_orig:
+ self.assertEqual(widget[name], orig)
+ else:
+ widget[name] = orig
+ with self.assertRaises(tkinter.TclError) as cm:
+ widget.configure({name: value})
+ if errmsg is not None:
+ self.assertEqual(str(cm.exception), errmsg)
+ if keep_orig:
+ self.assertEqual(widget[name], orig)
+ else:
+ widget[name] = orig
+
+ def checkParams(self, widget, name, *values, **kwargs):
+ for value in values:
+ self.checkParam(widget, name, value, **kwargs)
+
+ def checkIntegerParam(self, widget, name, *values, **kwargs):
+ self.checkParams(widget, name, *values, **kwargs)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected integer but got ""')
+ self.checkInvalidParam(widget, name, '10p',
+ errmsg='expected integer but got "10p"')
+ self.checkInvalidParam(widget, name, 3.2,
+ errmsg='expected integer but got "3.2"')
+
+ def checkFloatParam(self, widget, name, *values, conv=float, **kwargs):
+ for value in values:
+ self.checkParam(widget, name, value, conv=conv, **kwargs)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected floating-point number but got ""')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='expected floating-point number but got "spam"')
+
+ def checkBooleanParam(self, widget, name):
+ for value in (False, 0, 'false', 'no', 'off'):
+ self.checkParam(widget, name, value, expected=0)
+ for value in (True, 1, 'true', 'yes', 'on'):
+ self.checkParam(widget, name, value, expected=1)
+ self.checkInvalidParam(widget, name, '',
+ errmsg='expected boolean value but got ""')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='expected boolean value but got "spam"')
+
+ def checkColorParam(self, widget, name, *, allow_empty=None, **kwargs):
+ self.checkParams(widget, name,
+ '#ff0000', '#00ff00', '#0000ff', '#123456',
+ 'red', 'green', 'blue', 'white', 'black', 'grey',
+ **kwargs)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='unknown color name "spam"')
+
+ def checkCursorParam(self, widget, name, **kwargs):
+ self.checkParams(widget, name, 'arrow', 'watch', 'cross', '',**kwargs)
+ if tcl_version >= (8, 5):
+ self.checkParam(widget, name, 'none')
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='bad cursor spec "spam"')
+
+ def checkCommandParam(self, widget, name):
+ def command(*args):
+ pass
+ widget[name] = command
+ self.assertTrue(widget[name])
+ self.checkParams(widget, name, '')
+
+ def checkEnumParam(self, widget, name, *values, errmsg=None, **kwargs):
+ self.checkParams(widget, name, *values, **kwargs)
+ if errmsg is None:
+ errmsg2 = ' %s "{}": must be %s%s or %s' % (
+ name,
+ ', '.join(values[:-1]),
+ ',' if len(values) > 2 else '',
+ values[-1])
+ self.checkInvalidParam(widget, name, '',
+ errmsg='ambiguous' + errmsg2)
+ errmsg = 'bad' + errmsg2
+ self.checkInvalidParam(widget, name, 'spam', errmsg=errmsg)
+
+ def checkPixelsParam(self, widget, name, *values,
+ conv=None, keep_orig=True, **kwargs):
+ if conv is None:
+ conv = self._conv_pixels
+ for value in values:
+ expected = _sentinel
+ conv1 = conv
+ if isinstance(value, str):
+ if conv1 and conv1 is not str:
+ expected = pixels_conv(value) * self.scaling
+ conv1 = round
+ self.checkParam(widget, name, value, expected=expected,
+ conv=conv1, **kwargs)
+ self.checkInvalidParam(widget, name, '6x',
+ errmsg='bad screen distance "6x"', keep_orig=keep_orig)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='bad screen distance "spam"', keep_orig=keep_orig)
+
+ def checkReliefParam(self, widget, name):
+ self.checkParams(widget, name,
+ 'flat', 'groove', 'raised', 'ridge', 'solid', 'sunken')
+ errmsg='bad relief "spam": must be '\
+ 'flat, groove, raised, ridge, solid, or sunken'
+ if tcl_version < (8, 6):
+ errmsg = None
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg=errmsg)
+
+ def checkImageParam(self, widget, name):
+ image = tkinter.PhotoImage('image1')
+ self.checkParam(widget, name, image, conv=str)
+ self.checkInvalidParam(widget, name, 'spam',
+ errmsg='image "spam" doesn\'t exist')
+ widget[name] = ''
+
+ def checkVariableParam(self, widget, name, var):
+ self.checkParam(widget, name, var, conv=str)
+
+
+class StandardOptionsTests:
+ STANDARD_OPTIONS = (
+ 'activebackground', 'activeborderwidth', 'activeforeground', 'anchor',
+ 'background', 'bitmap', 'borderwidth', 'compound', 'cursor',
+ 'disabledforeground', 'exportselection', 'font', 'foreground',
+ 'highlightbackground', 'highlightcolor', 'highlightthickness',
+ 'image', 'insertbackground', 'insertborderwidth',
+ 'insertofftime', 'insertontime', 'insertwidth',
+ 'jump', 'justify', 'orient', 'padx', 'pady', 'relief',
+ 'repeatdelay', 'repeatinterval',
+ 'selectbackground', 'selectborderwidth', 'selectforeground',
+ 'setgrid', 'takefocus', 'text', 'textvariable', 'troughcolor',
+ 'underline', 'wraplength', 'xscrollcommand', 'yscrollcommand',
+ )
+
+ def test_activebackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'activebackground')
+
+ def test_activeborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'activeborderwidth',
+ 0, 1.3, 2.9, 6, -2, '10p')
+
+ def test_activeforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'activeforeground')
+
+ def test_anchor(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'anchor',
+ 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'center')
+
+ def test_background(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'background')
+ if 'bg' in self.OPTIONS:
+ self.checkColorParam(widget, 'bg')
+
+ def test_bitmap(self):
+ widget = self.create()
+ self.checkParam(widget, 'bitmap', 'questhead')
+ self.checkParam(widget, 'bitmap', 'gray50')
+ self.checkInvalidParam(widget, 'bitmap', 'spam',
+ errmsg='bitmap "spam" not defined')
+
+ def test_borderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'borderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+ if 'bd' in self.OPTIONS:
+ self.checkPixelsParam(widget, 'bd', 0, 1.3, 2.6, 6, -2, '10p')
+
+ def test_compound(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'compound',
+ 'bottom', 'center', 'left', 'none', 'right', 'top')
+
+ def test_cursor(self):
+ widget = self.create()
+ self.checkCursorParam(widget, 'cursor')
+
+ def test_disabledforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'disabledforeground')
+
+ def test_exportselection(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'exportselection')
+
+ def test_font(self):
+ widget = self.create()
+ self.checkParam(widget, 'font',
+ '-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*')
+ self.checkInvalidParam(widget, 'font', '',
+ errmsg='font "" doesn\'t exist')
+
+ def test_foreground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'foreground')
+ if 'fg' in self.OPTIONS:
+ self.checkColorParam(widget, 'fg')
+
+ def test_highlightbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'highlightbackground')
+
+ def test_highlightcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'highlightcolor')
+
+ def test_highlightthickness(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'highlightthickness',
+ 0, 1.3, 2.6, 6, '10p')
+ self.checkParam(widget, 'highlightthickness', -2, expected=0,
+ conv=self._conv_pixels)
+
+ def test_image(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'image')
+
+ def test_insertbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'insertbackground')
+
+ def test_insertborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertborderwidth',
+ 0, 1.3, 2.6, 6, -2, '10p')
+
+ def test_insertofftime(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'insertofftime', 100)
+
+ def test_insertontime(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'insertontime', 100)
+
+ def test_insertwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'insertwidth', 1.3, 2.6, -2, '10p')
+
+ def test_jump(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'jump')
+
+ def test_justify(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'justify', 'left', 'right', 'center',
+ errmsg='bad justification "{}": must be '
+ 'left, right, or center')
+ self.checkInvalidParam(widget, 'justify', '',
+ errmsg='ambiguous justification "": must be '
+ 'left, right, or center')
+
+ def test_orient(self):
+ widget = self.create()
+ self.assertEqual(str(widget['orient']), self.default_orient)
+ self.checkEnumParam(widget, 'orient', 'horizontal', 'vertical')
+
+ def test_padx(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'padx', 3, 4.4, 5.6, -2, '12m',
+ conv=self._conv_pad_pixels)
+
+ def test_pady(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'pady', 3, 4.4, 5.6, -2, '12m',
+ conv=self._conv_pad_pixels)
+
+ def test_relief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'relief')
+
+ def test_repeatdelay(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'repeatdelay', -500, 500)
+
+ def test_repeatinterval(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'repeatinterval', -500, 500)
+
+ def test_selectbackground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectbackground')
+
+ def test_selectborderwidth(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'selectborderwidth', 1.3, 2.6, -2, '10p')
+
+ def test_selectforeground(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectforeground')
+
+ def test_setgrid(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'setgrid')
+
+ def test_state(self):
+ widget = self.create()
+ self.checkEnumParam(widget, 'state', 'active', 'disabled', 'normal')
+
+ def test_takefocus(self):
+ widget = self.create()
+ self.checkParams(widget, 'takefocus', '0', '1', '')
+
+ def test_text(self):
+ widget = self.create()
+ self.checkParams(widget, 'text', '', 'any string')
+
+ def test_textvariable(self):
+ widget = self.create()
+ var = tkinter.StringVar()
+ self.checkVariableParam(widget, 'textvariable', var)
+
+ def test_troughcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'troughcolor')
+
+ def test_underline(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'underline', 0, 1, 10)
+
+ def test_wraplength(self):
+ widget = self.create()
+ if tcl_version < (8, 5):
+ self.checkPixelsParam(widget, 'wraplength', 100)
+ else:
+ self.checkParams(widget, 'wraplength', 100)
+
+ def test_xscrollcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'xscrollcommand')
+
+ def test_yscrollcommand(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'yscrollcommand')
+
+ # non-standard but common options
+
+ def test_command(self):
+ widget = self.create()
+ self.checkCommandParam(widget, 'command')
+
+ def test_indicatoron(self):
+ widget = self.create()
+ self.checkBooleanParam(widget, 'indicatoron')
+
+ def test_offrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'offrelief')
+
+ def test_overrelief(self):
+ widget = self.create()
+ self.checkReliefParam(widget, 'overrelief')
+
+ def test_selectcolor(self):
+ widget = self.create()
+ self.checkColorParam(widget, 'selectcolor')
+
+ def test_selectimage(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'selectimage')
+
+ @requires_tcl(8, 5)
+ def test_tristateimage(self):
+ widget = self.create()
+ self.checkImageParam(widget, 'tristateimage')
+
+ @requires_tcl(8, 5)
+ def test_tristatevalue(self):
+ widget = self.create()
+ self.checkParam(widget, 'tristatevalue', 'unknowable')
+
+ def test_variable(self):
+ widget = self.create()
+ var = tkinter.DoubleVar()
+ self.checkVariableParam(widget, 'variable', var)
+
+
+class IntegerSizeTests:
+ def test_height(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'height', 100, -100, 0)
+
+ def test_width(self):
+ widget = self.create()
+ self.checkIntegerParam(widget, 'width', 402, -402, 0)
+
+
+class PixelSizeTests:
+ def test_height(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'height', 100, 101.2, 102.6, -100, 0, '3c')
+
+ def test_width(self):
+ widget = self.create()
+ self.checkPixelsParam(widget, 'width', 402, 403.4, 404.6, -402, 0, '5i')
+
+
+def add_standard_options(*source_classes):
+ # This decorator adds test_xxx methods from source classes for every xxx
+ # option in the OPTIONS class attribute if they are not defined explicitly.
+ def decorator(cls):
+ for option in cls.OPTIONS:
+ methodname = 'test_' + option
+ if not hasattr(cls, methodname):
+ for source_class in source_classes:
+ if hasattr(source_class, methodname):
+ setattr(cls, methodname,
+ getattr(source_class, methodname))
+ break
+ else:
+ def test(self, option=option):
+ widget = self.create()
+ widget[option]
+ raise AssertionError('Option "%s" is not tested in %s' %
+ (option, cls.__name__))
+ test.__name__ = methodname
+ setattr(cls, methodname, test)
+ return cls
+ return decorator
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,11 @@
- Issue #19435: Fix directory traversal attack on CGIHttpRequestHandler.
+Tests
+-----
+
+- Issue #19085: Added basic tests for all tkinter widget options.
+
What's New in Python 3.3.3 release candidate 1?
===============================================
--
Repository URL: http://hg.python.org/cpython
1
0
results for 123804a72a8f on branch "default"
--------------------------------------------
test_asyncio leaked [0, 0, 4] memory blocks, sum=4
test_site leaked [2, -2, 2] references, sum=2
test_site leaked [2, -2, 2] memory blocks, sum=2
Command line was: ['./python', '-m', 'test.regrtest', '-uall', '-R', '3:3:/home/antoine/cpython/refleaks/reflogDVDFGr', '-x']
1
0
http://hg.python.org/peps/rev/1ad0ddb3070b
changeset: 5245:1ad0ddb3070b
user: Eric Snow <ericsnowcurrently(a)gmail.com>
date: Fri Nov 01 16:33:49 2013 -0600
summary:
[PEP 451] It pays to run make before committing.
files:
pep-0451.txt | 44 ++++++++++++++++-----------------------
1 files changed, 18 insertions(+), 26 deletions(-)
diff --git a/pep-0451.txt b/pep-0451.txt
--- a/pep-0451.txt
+++ b/pep-0451.txt
@@ -35,7 +35,7 @@
with which people may not be so familiar. For the sake of context, here
is a brief summary of all three groups of terms and concepts. A more
detailed explanation of the import system is found at
-[import_system_docs]_.
+[#import_system_docs]_.
name
----
@@ -180,7 +180,7 @@
there's an API void between finders and loaders that causes undue
complexity when encountered. The PEP 420 (namespace packages)
implementation had to work around this. The complexity surfaced again
-during recent efforts on a separate proposal. [ref_files_pep]_
+during recent efforts on a separate proposal. [#ref_files_pep]_
The `finder`_ and `loader`_ sections above detail current responsibility
of both. Notably, loaders are not required to provide any of the
@@ -524,8 +524,8 @@
`the "existing" parameter <The "existing" parameter of find_spec()>`_.
The semantics of reload will remain essentially the same as they exist
-already [reload-semantics-fix]_. The impact of this PEP on some kinds
-of lazy loading modules was a point of discussion. [lazy_import_concerns]_
+already [#reload-semantics-fix]_. The impact of this PEP on some kinds
+of lazy loading modules was a point of discussion. [#lazy_import_concerns]_
ModuleSpec
@@ -737,15 +737,15 @@
Two alternatives were presented to the "existing" parameter:
Loader.supports_reload() and adding "existing" to Loader.exec_module()
instead of find_spec(). supports_reload() was the initial approach to
-the reload situation. [supports_reload]_ However, there was some
+the reload situation. [#supports_reload]_ However, there was some
opposition to the loader-specific, reload-centric approach.
-[supports_reload_considered_harmful]_
+[#supports_reload_considered_harmful]_
As to "existing" on exec_module(), the loader may need other information
from the existing module (or spec) during reload, more than just "does
this loader support reloading this module", that is no longer available
with the move away from load_module(). A proposal on the table was to
-add something like "existing" to exec_module(). [exec_module_existing]_
+add something like "existing" to exec_module(). [#exec_module_existing]_
However, putting "existing" on find_spec() instead is more in line with
the goals of this PEP. Furthermore, it obviates the need for
supports_reload().
@@ -838,7 +838,7 @@
Open Issues
===========
-* In the `Finders`_ section, the PEP specifies returning None (or using
+\* In the `Finders`_ section, the PEP specifies returning None (or using
a different loader) when the found loader does not support loading into
an existing module (e.g during reload). An alternative to returning
None would be to raise ImportError with a message like "the loader does
@@ -885,16 +885,6 @@
the artificial (and mostly erroneous) distinction between modules and
packages.
-Others left out:
-
-* Add ModuleSpec.submodules (RO-property) - returns possible submodules
- relative to the spec.
-* Add ModuleSpec.loaded (RO-property) - the module in sys.module, if
- any.
-* Add ModuleSpec.data - a descriptor that wraps the data API of the
- spec's loader.
-* Also see [cleaner_reload_support]_.
-
The module spec `Factory Functions`_ could be classmethods on
ModuleSpec. However that would expose them on *all* modules via
``__spec__``, which has the potential to unnecessarily confuse
@@ -929,29 +919,31 @@
any.
* Add ModuleSpec.data - a descriptor that wraps the data API of the
spec's loader.
-* Also see [cleaner_reload_support]_.
+* Also see [#cleaner_reload_support]_.
References
==========
-.. [ref_files_pep]
+.. [#ref_files_pep]
http://mail.python.org/pipermail/import-sig/2013-August/000658.html
-.. [import_system_docs] http://docs.python.org/3/reference/import.html
+.. [#import_system_docs] http://docs.python.org/3/reference/import.html
-.. [cleaner_reload_support]
+.. [#cleaner_reload_support]
https://mail.python.org/pipermail/import-sig/2013-September/000735.html
-.. [lazy_import_concerns]
+.. [#lazy_import_concerns]
https://mail.python.org/pipermail/python-dev/2013-August/128129.html
-.. [reload-semantics-fix] http://bugs.python.org/issue19413
+.. [#reload-semantics-fix] http://bugs.python.org/issue19413
-.. [supports_reload_considered_harmful]
+.. [#supports_reload]
+ https://mail.python.org/pipermail/python-dev/2013-October/129913.html
+.. [#supports_reload_considered_harmful]
https://mail.python.org/pipermail/python-dev/2013-October/129971.html
-.. [exec_module_existing]
+.. [#exec_module_existing]
https://mail.python.org/pipermail/python-dev/2013-October/129933.html
Copyright
--
Repository URL: http://hg.python.org/peps
1
0

peps: [PEP 451] Update the signature of find_spec() and remove supports_reload().
by eric.snow Nov. 1, 2013
by eric.snow Nov. 1, 2013
Nov. 1, 2013
http://hg.python.org/peps/rev/845e08302be8
changeset: 5244:845e08302be8
user: Eric Snow <ericsnowcurrently(a)gmail.com>
date: Fri Nov 01 16:19:06 2013 -0600
summary:
[PEP 451] Update the signature of find_spec() and remove supports_reload().
files:
pep-0451.txt | 238 +++++++++++++++++++++++++-------------
1 files changed, 156 insertions(+), 82 deletions(-)
diff --git a/pep-0451.txt b/pep-0451.txt
--- a/pep-0451.txt
+++ b/pep-0451.txt
@@ -278,9 +278,9 @@
Other API Additions
-------------------
-* importlib.find_spec(name, path=None) will work exactly the same as
- importlib.find_loader() (which it replaces), but return a spec instead
- of a loader.
+* importlib.find_spec(name, path=None, existing=None) will work exactly
+ the same as importlib.find_loader() (which it replaces), but return a
+ spec instead of a loader.
For finders:
@@ -295,8 +295,6 @@
over its module execution functionality.
* importlib.abc.Loader.create_module(spec) (optional) will return the
module to use for loading.
-* importlib.abc.Loader.supports_reload(name) (optional) will return True
- (the default) if the loader supports reloading the module.
For modules:
@@ -374,13 +372,13 @@
finders:
-* create loader
-* create spec
+* create/identify a loader that can load the module.
+* create the spec for the module.
loaders:
-* create module (optional)
-* execute module
+* create the module (optional).
+* execute the module.
ModuleSpec:
@@ -404,11 +402,9 @@
* Implement exec_module() on loaders, if possible.
The ModuleSpec factory functions in importlib.util are intended to be
-helpful for converting existing finders. from_loader() and
-from_file_location() are both straight-forward utilities in this
-regard. In the case where loaders already expose methods for creating
-and preparing modules, ModuleSpec.from_module() may be useful to
-the corresponding finder.
+helpful for converting existing finders. spec_from_loader() and
+spec_from_file_location() are both straight-forward utilities in this
+regard.
For existing loaders, exec_module() should be a relatively direct
conversion from the non-boilerplate portion of load_module(). In some
@@ -497,7 +493,7 @@
name = module.__spec__.name
except AttributeError:
name = module.__name__
- spec = find_spec(name)
+ spec = find_spec(name, existing=module)
if sys.modules.get(name) is not module:
raise ImportError
@@ -509,8 +505,6 @@
# namespace loader
_init_module_attrs(spec, module)
return module
- if not spec.loader.supports_reload(name):
- raise ImportError
if spec.parent and spec.parent not in sys.modules:
raise ImportError
@@ -520,6 +514,19 @@
finally:
del _RELOADING[name]
+A key point here is the switch to Loader.exec_module() means that
+loaders will no longer have an easy way to know at execution time if it
+is a reload or not. Before this proposal, they could simply check to
+see if the module was already in sys.modules. Now, by the time
+exec_module() is called during load (not reload) the import machinery
+would already have placed the module in sys.modules. This is part of
+the reason why find_spec() has
+`the "existing" parameter <The "existing" parameter of find_spec()>`_.
+
+The semantics of reload will remain essentially the same as they exist
+already [reload-semantics-fix]_. The impact of this PEP on some kinds
+of lazy loading modules was a point of discussion. [lazy_import_concerns]_
+
ModuleSpec
==========
@@ -627,7 +634,7 @@
* "submodule_search_locations" can be deduced from loader.is_package()
and from os.path.dirname(location) if location is a filename.
-**from_loader(name, loader, \*, origin=None, is_package=None)**
+**spec_from_loader(name, loader, \*, origin=None, is_package=None)**
Build a spec with missing information filled in by using loader APIs.
@@ -636,45 +643,6 @@
* "submodule_search_locations" can be deduced from loader.is_package()
and from os.path.dirname(location) if location is a filename.
-**spec_from_module(module, loader=None)**
-
-Build a spec based on the import-related attributes of an existing
-module. The spec attributes are set to the corresponding import-
-related module attributes. See the table in `Attributes`_.
-
-Omitted Attributes and Methods
-------------------------------
-
-There is no "PathModuleSpec" subclass of ModuleSpec that separates out
-has_location, cached, and submodule_search_locations. While that might
-make the separation cleaner, module objects don't have that distinction.
-ModuleSpec will support both cases equally well.
-
-While "is_package" would be a simple additional attribute (aliasing
-self.submodule_search_locations is not None), it perpetuates the
-artificial (and mostly erroneous) distinction between modules and
-packages.
-
-Conceivably, a ModuleSpec.load() method could optionally take a list of
-modules with which to interact instead of sys.modules. That
-capability is left out of this PEP, but may be pursued separately at
-some other time, including relative to PEP 406 (import engine).
-
-Likewise load() could be leveraged to implement multi-version
-imports. While interesting, doing so is outside the scope of this
-proposal.
-
-Others:
-
-* Add ModuleSpec.submodules (RO-property) - returns possible submodules
- relative to the spec.
-* Add ModuleSpec.loaded (RO-property) - the module in sys.module, if
- any.
-* Add ModuleSpec.data - a descriptor that wraps the data API of the
- spec's loader.
-* Also see [cleaner_reload_support]_.
-
-
Backward Compatibility
----------------------
@@ -722,15 +690,16 @@
Finders
-------
-Finders are still responsible for creating the loader. That loader will
+Finders are still responsible for identifying, an typically creating,
+the loader that should be used to load a module. That loader will
now be stored in the module spec returned by find_spec() rather
than returned directly. As is currently the case without the PEP, if a
loader would be costly to create, that loader can be designed to defer
the cost until later.
-**MetaPathFinder.find_spec(name, path=None)**
+**MetaPathFinder.find_spec(name, path=None, existing=None)**
-**PathEntryFinder.find_spec(name)**
+**PathEntryFinder.find_spec(name, existing=None)**
Finders must return ModuleSpec objects when find_spec() is
called. This new method replaces find_module() and
@@ -745,6 +714,42 @@
added in Python 3.3. However, the extra complexity and a less-than-
explicit method name aren't worth it.
+The "existing" parameter of find_spec()
+---------------------------------------
+
+A module object with the same name as the "name" argument (or None, the
+default) should be passed in to "exising". This argument allows the
+finder to build the module spec with more information than is otherwise
+available. This is particularly relevant in identifying the loader to
+use.
+
+Through find_spec() the finder will always identify the loader it
+will return in the spec. In the case of reload, at this point the
+finder should also decide whether or not the loader supports loading
+into the module-to-be-reloaded (which was passed in to find_spec() as
+"existing"). This decision may entail consulting with the loader. If
+the finder determines that the loader does not support reloading that
+module, it should either find another loader or return None (indicating
+that it could not "find" the module). This reload decision is important
+since, as noted in `How Reloading Will Work`_, loaders will no longer be
+able to trivially identify a reload situation on their own.
+
+Two alternatives were presented to the "existing" parameter:
+Loader.supports_reload() and adding "existing" to Loader.exec_module()
+instead of find_spec(). supports_reload() was the initial approach to
+the reload situation. [supports_reload]_ However, there was some
+opposition to the loader-specific, reload-centric approach.
+[supports_reload_considered_harmful]_
+
+As to "existing" on exec_module(), the loader may need other information
+from the existing module (or spec) during reload, more than just "does
+this loader support reloading this module", that is no longer available
+with the move away from load_module(). A proposal on the table was to
+add something like "existing" to exec_module(). [exec_module_existing]_
+However, putting "existing" on find_spec() instead is more in line with
+the goals of this PEP. Furthermore, it obviates the need for
+supports_reload().
+
Namespace Packages
------------------
@@ -791,13 +796,6 @@
module attributes. The fact that load_module() does is a design flaw
that this proposal aims to correct.
-**Loader.supports_reload(name)**
-
-In cases where a module should not be reloaded, Loaders should implement
-supports_reload() and have it return False. If the method is defined
-and returns a false value, importlib.reload() will raise an ImportError.
-Otherwise reloading proceeds as normal.
-
Other changes:
PEP 420 introduced the optional module_repr() loader method to limit
@@ -837,24 +835,27 @@
* importlib.reload() will now make use of the per-module import lock.
+Open Issues
+===========
+
+* In the `Finders`_ section, the PEP specifies returning None (or using
+a different loader) when the found loader does not support loading into
+an existing module (e.g during reload). An alternative to returning
+None would be to raise ImportError with a message like "the loader does
+not support reloading the module". This may actually be a better
+approach since "could not find a loader" and "the found loader won't
+work" are different situations that a single return value (None) may not
+sufficiently represent.
+
+
Reference Implementation
========================
-A reference implementation will be available at
+A reference implementation is available at
http://bugs.python.org/issue18864.
-
-Open Issues
-==============
-
-\* Impact on some kinds of lazy loading modules. [lazy_import_concerns]_
-
-This should not be an issue since the PEP does not change the semantics
-of this behavior.
-
-
Implementation Notes
-====================
+--------------------
\* The implementation of this PEP needs to be cognizant of its impact on
pkgutil (and setuptools). pkgutil has some generic function-based
@@ -868,17 +869,90 @@
at ``module.__spec__.name``.
+Rejected Additions to the PEP
+=============================
+
+There were a few proposed additions to this proposal that did not fit
+well enough into its scope.
+
+There is no "PathModuleSpec" subclass of ModuleSpec that separates out
+has_location, cached, and submodule_search_locations. While that might
+make the separation cleaner, module objects don't have that distinction.
+ModuleSpec will support both cases equally well.
+
+While "ModuleSpec.is_package" would be a simple additional attribute
+(aliasing self.submodule_search_locations is not None), it perpetuates
+the artificial (and mostly erroneous) distinction between modules and
+packages.
+
+Others left out:
+
+* Add ModuleSpec.submodules (RO-property) - returns possible submodules
+ relative to the spec.
+* Add ModuleSpec.loaded (RO-property) - the module in sys.module, if
+ any.
+* Add ModuleSpec.data - a descriptor that wraps the data API of the
+ spec's loader.
+* Also see [cleaner_reload_support]_.
+
+The module spec `Factory Functions`_ could be classmethods on
+ModuleSpec. However that would expose them on *all* modules via
+``__spec__``, which has the potential to unnecessarily confuse
+non-advanced Python users. The factory functions have a specific use
+case, to support finder authors. See `ModuleSpec Users`_.
+
+Likewise, several other methods could be added to ModuleSpec that expose
+the specific uses of module specs by the import machinery:
+
+* create() - a wrapper around Loader.create_module().
+* exec(module) - a wrapper around Loader.exec_module().
+* load() - an analogue to the deprecated Loader.load_module().
+
+As with the factory functions, exposing these methods via
+module.__spec__ is less than desireable. They would end up being an
+attractive nuisance, even if only exposed as "private" attributes (as
+they were in previous versions of this PEP). If someone finds a need
+for these methods later, we can expose the via an appropriate API
+(separate from ModuleSpec) at that point, perhaps relative to PEP 406
+(import engine).
+
+Conceivably, the load() method could optionally take a list of
+modules with which to interact instead of sys.modules. Also, load()
+could be leveraged to implement multi-version imports. Both are
+interesting ideas, but definitely outside the scope of this proposal.
+
+Others left out:
+
+* Add ModuleSpec.submodules (RO-property) - returns possible submodules
+ relative to the spec.
+* Add ModuleSpec.loaded (RO-property) - the module in sys.module, if
+ any.
+* Add ModuleSpec.data - a descriptor that wraps the data API of the
+ spec's loader.
+* Also see [cleaner_reload_support]_.
+
+
References
==========
-.. [ref_files_pep] http://mail.python.org/pipermail/import-sig/2013-August/000658.html
+.. [ref_files_pep]
+ http://mail.python.org/pipermail/import-sig/2013-August/000658.html
.. [import_system_docs] http://docs.python.org/3/reference/import.html
-.. [cleaner_reload_support] https://mail.python.org/pipermail/import-sig/2013-September/000735.html
+.. [cleaner_reload_support]
+ https://mail.python.org/pipermail/import-sig/2013-September/000735.html
-.. [lazy_import_concerns] https://mail.python.org/pipermail/python-dev/2013-August/128129.html
+.. [lazy_import_concerns]
+ https://mail.python.org/pipermail/python-dev/2013-August/128129.html
+.. [reload-semantics-fix] http://bugs.python.org/issue19413
+
+.. [supports_reload_considered_harmful]
+ https://mail.python.org/pipermail/python-dev/2013-October/129971.html
+
+.. [exec_module_existing]
+ https://mail.python.org/pipermail/python-dev/2013-October/129933.html
Copyright
=========
--
Repository URL: http://hg.python.org/peps
1
0
http://hg.python.org/peps/rev/c12ea420a840
changeset: 5243:c12ea420a840
user: Guido van Rossum <guido(a)dropbox.com>
date: Fri Nov 01 15:14:39 2013 -0700
summary:
Resolve a few TBD/TODOs.
files:
pep-3156.txt | 70 ++++++++++++++++-----------------------
1 files changed, 29 insertions(+), 41 deletions(-)
diff --git a/pep-3156.txt b/pep-3156.txt
--- a/pep-3156.txt
+++ b/pep-3156.txt
@@ -740,8 +740,7 @@
``None``. Note: the name uses ``sendall`` instead of ``send``, to
reflect that the semantics and signature of this method echo those
of the standard library socket method ``sendall()`` rather than
- ``send()``. (TBD: but maybe it would be better to emulate
- ``send()`` after all? That would be better for datagram sockets.)
+ ``send()``.
- ``sock_connect(sock, address)``. Connect to the given address.
Returns a Future whose result on success will be ``None``.
@@ -962,11 +961,16 @@
``concurrent.futures.Future`` is explicitly mentioned. The supported
public API is as follows, indicating the differences with PEP 3148:
-- ``cancel()``. If the Future is already done (or cancelled), return
- ``False``. Otherwise, change the Future's state to cancelled (this
- implies done), schedule the callbacks, and return ``True``.
+- ``cancel()``. If the Future is already done (or cancelled), do
+ nothing and return ``False``. Otherwise, this attempts to cancel
+ the Future and returns ``True``. If the the cancellation attempt is
+ successful, eventually the Future's state will change to cancelled
+ and the callbacks will be scheduled. For regular Futures,
+ cancellation will always succeed immediately; but for Tasks (see
+ below) the task may ignore or delay the cancellation attempt.
-- ``cancelled()``. Returns ``True`` if the Future was cancelled.
+- ``cancelled()``. Returns ``True`` if the Future was successfully
+ cancelled.
- ``done()``. Returns ``True`` if the Future is done. Note that a
cancelled Future is considered done too (here and everywhere).
@@ -1031,8 +1035,7 @@
- ``TimeoutError``. An alias for ``concurrent.futures.TimeoutError``.
May be raised by ``run_until_complete()``.
-A Future is associated with the default event loop when it is created.
-(TBD: Optionally pass in an alternative event loop instance?)
+A Future is associated with an event loop when it is created.
A ``asyncio.Future`` object is not acceptable to the ``wait()`` and
``as_completed()`` functions in the ``concurrent.futures`` package.
@@ -1045,9 +1048,10 @@
and the Scheduler" below.
When a Future is garbage-collected, if it has an associated exception
-but neither ``result()`` nor ``exception()`` nor ``__iter__()`` has
-ever been called (or the latter hasn't raised the exception yet --
-details TBD), the exception is logged.
+but neither ``result()`` nor ``exception()`` has ever been called, the
+exception is logged. (When a coroutine uses ``yield from`` to wait
+for a Future, that Future's ``result()`` method is called once the
+coroutine is resumed.)
In the future (pun intended) we may unify ``asyncio.Future`` and
``concurrent.futures.Future``, e.g. by adding an ``__iter__()`` method
@@ -1171,10 +1175,13 @@
- ``pause_reading()``. Suspend delivery of data to the protocol until
a subsequent ``resume_reading()`` call. Between ``pause_reading()``
and ``resume_reading()``, the protocol's ``data_received()`` method
- will not be called. This has no effect on ``write()``.
+ will not be called.
- ``resume_reading()``. Restart delivery of data to the protocol via
- ``data_received()``.
+ ``data_received()``. Note that "paused" is a binary state --
+ ``pause_reading()`` should only be called when the transport is not
+ paused, while ``resume_reading()`` should only be called when the
+ transport is paused.
- ``close()``. Sever the connection with the entity at the other end.
Any data buffered by ``write()`` will (eventually) be transferred
@@ -1187,17 +1194,7 @@
- ``abort()``. Immediately sever the connection. Any data still
buffered by the transport is thrown away. Soon, the protocol's
``connection_lost()`` method will be called with ``None`` as
- argument. (TBD: Distinguish in the ``connection_lost()`` argument
- between ``close()``, ``abort()`` or a close initated by the other
- end? Or add a transport method to inquire about this? Glyph's
- proposal was to pass different exceptions for this purpose.)
-
-TBD: Provide flow control the other way -- the transport may need to
-suspend the protocol if the amount of data buffered becomes a burden.
-Proposal: let the transport call ``protocol.pause_writing()`` and
-``protocol.resume_writing()`` if they exist; if they don't exist, the
-protocol doesn't support flow control. (Perhaps different names
-to avoid confusion between protocols and transports?)
+ argument.
Unidirectional Stream Transports
''''''''''''''''''''''''''''''''
@@ -1245,8 +1242,8 @@
transport is closed. The ``connection_refused()`` method is called
before ``connection_lost()`` when ``remote_addr`` was given and an
explicit negative acknowledgement was received (this is a UDP
-feature). (TBD: Do we need the latter? It seems easy enough to
-implement this in the protocol if it needs to make the distinction.)
+feature). (TBD: Should fix `connection_refused()`` to not close the
+transport.)
Subprocess Transports
'''''''''''''''''''''
@@ -1267,8 +1264,6 @@
of protocol is a bidirectional stream protocol. (There are no
unidirectional protocols.)
-(TBD: should protocol callbacks be allowed to be coroutines?)
-
Stream Protocols
''''''''''''''''
@@ -1296,6 +1291,9 @@
``write_eof()`` (or something equivalent). If this returns a false
value (including None), the transport will close itself. If it
returns a true value, closing the transport is up to the protocol.
+ However, for SSL/TLS connections this is ignored, because the TLS
+ standard requires that no more data is sent and the connection is
+ closed as soon as a "closure alert" is received.
The default implementation returns None.
@@ -1303,8 +1301,7 @@
has detected that the other end has closed the connection cleanly,
or has encountered an unexpected error. In the first three cases
the argument is ``None``; for an unexpected error, the argument is
- the exception that caused the transport to give up. (TBD: Do we
- need to distinguish between the first three cases?)
+ the exception that caused the transport to give up.
Here is a chart indicating the order and multiplicity of calls:
@@ -1313,8 +1310,7 @@
3. ``eof_received()`` -- at most once
4. ``connection_lost()`` -- exactly once
-TBD: Discuss whether user code needs to do anything to make sure that
-protocol and transport aren't garbage-collected prematurely.
+TBD: Document ``pause_writing()`` and ``resume_writing()``.
Datagram Protocols
''''''''''''''''''
@@ -1502,9 +1498,6 @@
The coroutine ``asyncio.sleep(delay)`` returns after a given time delay.
-(TBD: Should the optional second argument, ``result``, be part of the
-spec?)
-
Tasks
-----
@@ -1565,17 +1558,12 @@
TO DO
=====
-- Document pause/resume reading/writing.
+- Document pause/resume_writing.
- Document subprocess/pipe protocols/transports.
- Document locks and queues.
-- Describe task cancellation details (and update future cancellation
- spec to promise less).
-
-- Explain that eof_received() shouldn't return True with SSL/TLS.
-
- Document SIGCHILD handling API (once it lands).
- Compare all APIs with the source code to be sure there aren't any
--
Repository URL: http://hg.python.org/peps
1
0

cpython: asyncio: Slight rearrangement of tests for server_hostname=...
by guido.van.rossum Nov. 1, 2013
by guido.van.rossum Nov. 1, 2013
Nov. 1, 2013
http://hg.python.org/cpython/rev/123804a72a8f
changeset: 86833:123804a72a8f
user: Guido van Rossum <guido(a)dropbox.com>
date: Fri Nov 01 14:24:28 2013 -0700
summary:
asyncio: Slight rearrangement of tests for server_hostname=...
files:
Lib/test/test_asyncio/test_base_events.py | 7 ++++---
1 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -444,7 +444,7 @@
self.assertRaises(
OSError, self.loop.run_until_complete, coro)
- def test_create_connection_server_hostname_default(self):
+ def test_create_connection_ssl_server_hostname_default(self):
self.loop.getaddrinfo = unittest.mock.Mock()
def mock_getaddrinfo(*args, **kwds):
@@ -490,8 +490,8 @@
server_side=False,
server_hostname='')
- def test_create_connection_server_hostname_errors(self):
- # When not using ssl, server_hostname must be None (but '' is OK).
+ def test_create_connection_no_ssl_server_hostname_errors(self):
+ # When not using ssl, server_hostname must be None.
coro = self.loop.create_connection(MyProto, 'python.org', 80,
server_hostname='')
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
@@ -499,6 +499,7 @@
server_hostname='python.org')
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
+ def test_create_connection_ssl_server_hostname_errors(self):
# When using ssl, server_hostname may be None if host is non-empty.
coro = self.loop.create_connection(MyProto, '', 80, ssl=True)
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
--
Repository URL: http://hg.python.org/cpython
1
0