Skip to content

Commit f12796c

Browse files
committed
#21940: Add unittest for WidgetRedirector. Initial patch by Saimadhav Heblikar.
1 parent 09197b3 commit f12796c

File tree

3 files changed

+145
-13
lines changed

3 files changed

+145
-13
lines changed

Lib/idlelib/WidgetRedirector.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
class WidgetRedirector:
44
"""Support for redirecting arbitrary widget subcommands.
55
6-
Some Tk operations don't normally pass through Tkinter. For example, if a
6+
Some Tk operations don't normally pass through tkinter. For example, if a
77
character is inserted into a Text widget by pressing a key, a default Tk
88
binding to the widget's 'insert' operation is activated, and the Tk library
9-
processes the insert without calling back into Tkinter.
9+
processes the insert without calling back into tkinter.
1010
11-
Although a binding to <Key> could be made via Tkinter, what we really want
12-
to do is to hook the Tk 'insert' operation itself.
11+
Although a binding to <Key> could be made via tkinter, what we really want
12+
to do is to hook the Tk 'insert' operation itself. For one thing, we want
13+
a text.insert call in idle code to have the same effect as a key press.
1314
1415
When a widget is instantiated, a Tcl command is created whose name is the
1516
same as the pathname widget._w. This command is used to invoke the various
1617
widget operations, e.g. insert (for a Text widget). We are going to hook
1718
this command and provide a facility ('register') to intercept the widget
18-
operation.
19+
operation. We will also intercept method calls on the tkinter class
20+
instance that represents the tk widget.
1921
2022
In IDLE, WidgetRedirector is used in Percolator to intercept Text
2123
commands. The function being registered provides access to the top
@@ -64,9 +66,13 @@ def close(self):
6466
def register(self, operation, function):
6567
'''Return OriginalCommand(operation) after registering function.
6668
67-
Registration adds an instance function attribute that masks the
68-
class instance method attribute. If a second function is
69-
registered for the same operation, the first function is replaced.
69+
Registration adds an operation: function pair to ._operations.
70+
It also adds an widget function attribute that masks the tkinter
71+
class instance method. Method masking operates independently
72+
from command dispatch.
73+
74+
If a second function is registered for the same operation, the
75+
first function is replaced in both places.
7076
'''
7177
self._operations[operation] = function
7278
setattr(self.widget, operation, function)
@@ -80,8 +86,10 @@ def unregister(self, operation):
8086
if operation in self._operations:
8187
function = self._operations[operation]
8288
del self._operations[operation]
83-
if hasattr(self.widget, operation):
89+
try:
8490
delattr(self.widget, operation)
91+
except AttributeError:
92+
pass
8593
return function
8694
else:
8795
return None
@@ -160,8 +168,7 @@ def my_insert(*args):
160168

161169
if __name__ == "__main__":
162170
import unittest
163-
## unittest.main('idlelib.idle_test.test_widgetredir',
164-
## verbosity=2, exit=False)
165-
171+
unittest.main('idlelib.idle_test.test_widgetredir',
172+
verbosity=2, exit=False)
166173
from idlelib.idle_test.htest import run
167174
run(_widget_redirector)

Lib/idlelib/idle_test/mock_idle.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def __call__(self, *args, **kwds):
2626
self.called = True
2727
self.args = args
2828
self.kwds = kwds
29-
return self.result
29+
if isinstance(self.result, BaseException):
30+
raise self.result
31+
else:
32+
return self.result
3033

3134

3235
class Editor:
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
"""Unittest for idlelib.WidgetRedirector
2+
3+
100% coverage
4+
"""
5+
from test.support import requires
6+
import unittest
7+
from idlelib.idle_test.mock_idle import Func
8+
from tkinter import Tk, Text, TclError
9+
from idlelib.WidgetRedirector import WidgetRedirector
10+
11+
12+
class InitCloseTest(unittest.TestCase):
13+
14+
@classmethod
15+
def setUpClass(cls):
16+
requires('gui')
17+
cls.tk = Tk()
18+
cls.text = Text(cls.tk)
19+
20+
@classmethod
21+
def tearDownClass(cls):
22+
cls.text.destroy()
23+
cls.tk.destroy()
24+
del cls.text, cls.tk
25+
26+
def test_init(self):
27+
redir = WidgetRedirector(self.text)
28+
self.assertEqual(redir.widget, self.text)
29+
self.assertEqual(redir.tk, self.text.tk)
30+
self.assertRaises(TclError, WidgetRedirector, self.text)
31+
redir.close() # restore self.tk, self.text
32+
33+
def test_close(self):
34+
redir = WidgetRedirector(self.text)
35+
redir.register('insert', Func)
36+
redir.close()
37+
self.assertEqual(redir._operations, {})
38+
self.assertFalse(hasattr(self.text, 'widget'))
39+
40+
41+
class WidgetRedirectorTest(unittest.TestCase):
42+
43+
@classmethod
44+
def setUpClass(cls):
45+
requires('gui')
46+
cls.tk = Tk()
47+
cls.text = Text(cls.tk)
48+
49+
@classmethod
50+
def tearDownClass(cls):
51+
cls.text.destroy()
52+
cls.tk.destroy()
53+
del cls.text, cls.tk
54+
55+
def setUp(self):
56+
self.redir = WidgetRedirector(self.text)
57+
self.func = Func()
58+
self.orig_insert = self.redir.register('insert', self.func)
59+
self.text.insert('insert', 'asdf') # leaves self.text empty
60+
61+
def tearDown(self):
62+
self.text.delete('1.0', 'end')
63+
self.redir.close()
64+
65+
def test_repr(self): # partly for 100% coverage
66+
self.assertIn('Redirector', repr(self.redir))
67+
self.assertIn('Original', repr(self.orig_insert))
68+
69+
def test_register(self):
70+
self.assertEqual(self.text.get('1.0', 'end'), '\n')
71+
self.assertEqual(self.func.args, ('insert', 'asdf'))
72+
self.assertIn('insert', self.redir._operations)
73+
self.assertIn('insert', self.text.__dict__)
74+
self.assertEqual(self.text.insert, self.func)
75+
76+
def test_original_command(self):
77+
self.assertEqual(self.orig_insert.operation, 'insert')
78+
self.assertEqual(self.orig_insert.tk_call, self.text.tk.call)
79+
self.orig_insert('insert', 'asdf')
80+
self.assertEqual(self.text.get('1.0', 'end'), 'asdf\n')
81+
82+
def test_unregister(self):
83+
self.assertIsNone(self.redir.unregister('invalid operation name'))
84+
self.assertEqual(self.redir.unregister('insert'), self.func)
85+
self.assertNotIn('insert', self.redir._operations)
86+
self.assertNotIn('insert', self.text.__dict__)
87+
88+
def test_unregister_no_attribute(self):
89+
del self.text.insert
90+
self.assertEqual(self.redir.unregister('insert'), self.func)
91+
92+
def test_dispatch_intercept(self):
93+
self.func.__init__(True)
94+
self.assertTrue(self.redir.dispatch('insert', False))
95+
self.assertFalse(self.func.args[0])
96+
97+
def test_dispatch_bypass(self):
98+
self.orig_insert('insert', 'asdf')
99+
# tk.call returns '' where Python would return None
100+
self.assertEqual(self.redir.dispatch('delete', '1.0', 'end'), '')
101+
self.assertEqual(self.text.get('1.0', 'end'), '\n')
102+
103+
def test_dispatch_error(self):
104+
self.func.__init__(TclError())
105+
self.assertEqual(self.redir.dispatch('insert', False), '')
106+
self.assertEqual(self.redir.dispatch('invalid'), '')
107+
108+
def test_command_dispatch(self):
109+
# Test that .__init__ causes redirection of tk calls
110+
# through redir.dispatch
111+
self.tk.call(self.text._w, 'insert', 'hello')
112+
self.assertEqual(self.func.args, ('hello',))
113+
self.assertEqual(self.text.get('1.0', 'end'), '\n')
114+
# Ensure that called through redir .dispatch and not through
115+
# self.text.insert by having mock raise TclError.
116+
self.func.__init__(TclError())
117+
self.assertEqual(self.tk.call(self.text._w, 'insert', 'boo'), '')
118+
119+
120+
121+
if __name__ == '__main__':
122+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)