Coverage for idle_test/test_redirector.py: 39%
92 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
1"Test redirector, coverage 100%."
3from idlelib.redirector import WidgetRedirector
4import unittest
5from test.support import requires
6from tkinter import Tk, Text, TclError
7from idlelib.idle_test.mock_idle import Func
10class InitCloseTest(unittest.TestCase):
12 @classmethod
13 def setUpClass(cls):
14 requires('gui')
15 cls.root = Tk()
16 cls.root.withdraw()
17 cls.text = Text(cls.root)
19 @classmethod
20 def tearDownClass(cls):
21 del cls.text
22 cls.root.destroy()
23 del cls.root
25 def test_init(self):
26 redir = WidgetRedirector(self.text)
27 self.assertEqual(redir.widget, self.text)
28 self.assertEqual(redir.tk, self.text.tk)
29 self.assertRaises(TclError, WidgetRedirector, self.text)
30 redir.close() # restore self.tk, self.text
32 def test_close(self):
33 redir = WidgetRedirector(self.text)
34 redir.register('insert', Func)
35 redir.close()
36 self.assertEqual(redir._operations, {})
37 self.assertFalse(hasattr(self.text, 'widget'))
40class WidgetRedirectorTest(unittest.TestCase):
42 @classmethod
43 def setUpClass(cls):
44 requires('gui')
45 cls.root = Tk()
46 cls.root.withdraw()
47 cls.text = Text(cls.root)
49 @classmethod
50 def tearDownClass(cls):
51 del cls.text
52 cls.root.update_idletasks()
53 cls.root.destroy()
54 del cls.root
56 def setUp(self):
57 self.redir = WidgetRedirector(self.text)
58 self.func = Func()
59 self.orig_insert = self.redir.register('insert', self.func)
60 self.text.insert('insert', 'asdf') # leaves self.text empty
62 def tearDown(self):
63 self.text.delete('1.0', 'end')
64 self.redir.close()
66 def test_repr(self): # partly for 100% coverage
67 self.assertIn('Redirector', repr(self.redir))
68 self.assertIn('Original', repr(self.orig_insert))
70 def test_register(self):
71 self.assertEqual(self.text.get('1.0', 'end'), '\n')
72 self.assertEqual(self.func.args, ('insert', 'asdf'))
73 self.assertIn('insert', self.redir._operations)
74 self.assertIn('insert', self.text.__dict__)
75 self.assertEqual(self.text.insert, self.func)
77 def test_original_command(self):
78 self.assertEqual(self.orig_insert.operation, 'insert')
79 self.assertEqual(self.orig_insert.tk_call, self.text.tk.call)
80 self.orig_insert('insert', 'asdf')
81 self.assertEqual(self.text.get('1.0', 'end'), 'asdf\n')
83 def test_unregister(self):
84 self.assertIsNone(self.redir.unregister('invalid operation name'))
85 self.assertEqual(self.redir.unregister('insert'), self.func)
86 self.assertNotIn('insert', self.redir._operations)
87 self.assertNotIn('insert', self.text.__dict__)
89 def test_unregister_no_attribute(self):
90 del self.text.insert
91 self.assertEqual(self.redir.unregister('insert'), self.func)
93 def test_dispatch_intercept(self):
94 self.func.__init__(True)
95 self.assertTrue(self.redir.dispatch('insert', False))
96 self.assertFalse(self.func.args[0])
98 def test_dispatch_bypass(self):
99 self.orig_insert('insert', 'asdf')
100 # tk.call returns '' where Python would return None
101 self.assertEqual(self.redir.dispatch('delete', '1.0', 'end'), '')
102 self.assertEqual(self.text.get('1.0', 'end'), '\n')
104 def test_dispatch_error(self):
105 self.func.__init__(TclError())
106 self.assertEqual(self.redir.dispatch('insert', False), '')
107 self.assertEqual(self.redir.dispatch('invalid'), '')
109 def test_command_dispatch(self):
110 # Test that .__init__ causes redirection of tk calls
111 # through redir.dispatch
112 self.root.call(self.text._w, 'insert', 'hello')
113 self.assertEqual(self.func.args, ('hello',))
114 self.assertEqual(self.text.get('1.0', 'end'), '\n')
115 # Ensure that called through redir .dispatch and not through
116 # self.text.insert by having mock raise TclError.
117 self.func.__init__(TclError())
118 self.assertEqual(self.root.call(self.text._w, 'insert', 'boo'), '')
121if __name__ == '__main__': 121 ↛ 122line 121 didn't jump to line 122, because the condition on line 121 was never true
122 unittest.main(verbosity=2)