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