ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_redirector.py

Python code coverage for Lib/idlelib/idle_test/test_redirector.py

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