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