How to use redir method in fMBT

Best Python code snippet using fMBT_python

test_redirector.py

Source:test_redirector.py Github

copy

Full Screen

1"Test redirector, coverage 100%."2from idlelib.redirector import WidgetRedirector3import unittest4from test.support import requires5from tkinter import Tk, Text, TclError6from idlelib.idle_test.mock_idle import Func7class InitCloseTest(unittest.TestCase):8 @classmethod9 def setUpClass(cls):10 requires('gui')11 cls.root = Tk()12 cls.root.withdraw()13 cls.text = Text(cls.root)14 @classmethod15 def tearDownClass(cls):16 del cls.text17 cls.root.destroy()18 del cls.root19 def test_init(self):20 redir = WidgetRedirector(self.text)21 self.assertEqual(redir.widget, self.text)22 self.assertEqual(redir.tk, self.text.tk)23 self.assertRaises(TclError, WidgetRedirector, self.text)24 redir.close() # restore self.tk, self.text25 def test_close(self):26 redir = WidgetRedirector(self.text)27 redir.register('insert', Func)28 redir.close()29 self.assertEqual(redir._operations, {})30 self.assertFalse(hasattr(self.text, 'widget'))31class WidgetRedirectorTest(unittest.TestCase):32 @classmethod33 def setUpClass(cls):34 requires('gui')35 cls.root = Tk()36 cls.root.withdraw()37 cls.text = Text(cls.root)38 @classmethod39 def tearDownClass(cls):40 del cls.text41 cls.root.update_idletasks()42 cls.root.destroy()43 del cls.root44 def setUp(self):45 self.redir = WidgetRedirector(self.text)46 self.func = Func()47 self.orig_insert = self.redir.register('insert', self.func)48 self.text.insert('insert', 'asdf') # leaves self.text empty49 def tearDown(self):50 self.text.delete('1.0', 'end')51 self.redir.close()52 def test_repr(self): # partly for 100% coverage53 self.assertIn('Redirector', repr(self.redir))54 self.assertIn('Original', repr(self.orig_insert))55 def test_register(self):56 self.assertEqual(self.text.get('1.0', 'end'), '\n')57 self.assertEqual(self.func.args, ('insert', 'asdf'))58 self.assertIn('insert', self.redir._operations)59 self.assertIn('insert', self.text.__dict__)60 self.assertEqual(self.text.insert, self.func)61 def test_original_command(self):62 self.assertEqual(self.orig_insert.operation, 'insert')63 self.assertEqual(self.orig_insert.tk_call, self.text.tk.call)64 self.orig_insert('insert', 'asdf')65 self.assertEqual(self.text.get('1.0', 'end'), 'asdf\n')66 def test_unregister(self):67 self.assertIsNone(self.redir.unregister('invalid operation name'))68 self.assertEqual(self.redir.unregister('insert'), self.func)69 self.assertNotIn('insert', self.redir._operations)70 self.assertNotIn('insert', self.text.__dict__)71 def test_unregister_no_attribute(self):72 del self.text.insert73 self.assertEqual(self.redir.unregister('insert'), self.func)74 def test_dispatch_intercept(self):75 self.func.__init__(True)76 self.assertTrue(self.redir.dispatch('insert', False))77 self.assertFalse(self.func.args[0])78 def test_dispatch_bypass(self):79 self.orig_insert('insert', 'asdf')80 # tk.call returns '' where Python would return None81 self.assertEqual(self.redir.dispatch('delete', '1.0', 'end'), '')82 self.assertEqual(self.text.get('1.0', 'end'), '\n')83 def test_dispatch_error(self):84 self.func.__init__(TclError())85 self.assertEqual(self.redir.dispatch('insert', False), '')86 self.assertEqual(self.redir.dispatch('invalid'), '')87 def test_command_dispatch(self):88 # Test that .__init__ causes redirection of tk calls89 # through redir.dispatch90 self.root.call(self.text._w, 'insert', 'hello')91 self.assertEqual(self.func.args, ('hello',))92 self.assertEqual(self.text.get('1.0', 'end'), '\n')93 # Ensure that called through redir .dispatch and not through94 # self.text.insert by having mock raise TclError.95 self.func.__init__(TclError())96 self.assertEqual(self.root.call(self.text._w, 'insert', 'boo'), '')97if __name__ == '__main__':...

Full Screen

Full Screen

test_widgetredir.py

Source:test_widgetredir.py Github

copy

Full Screen

1"""Unittest for idlelib.WidgetRedirector2100% coverage3"""4from test.test_support import requires5import unittest6from idlelib.idle_test.mock_idle import Func7from Tkinter import Tk, Text, TclError8from idlelib.WidgetRedirector import WidgetRedirector9class InitCloseTest(unittest.TestCase):10 @classmethod11 def setUpClass(cls):12 requires('gui')13 cls.tk = Tk()14 cls.text = Text(cls.tk)15 @classmethod16 def tearDownClass(cls):17 cls.text.destroy()18 cls.tk.destroy()19 del cls.text, cls.tk20 def test_init(self):21 redir = WidgetRedirector(self.text)22 self.assertEqual(redir.widget, self.text)23 self.assertEqual(redir.tk, self.text.tk)24 self.assertRaises(TclError, WidgetRedirector, self.text)25 redir.close() # restore self.tk, self.text26 def test_close(self):27 redir = WidgetRedirector(self.text)28 redir.register('insert', Func)29 redir.close()30 self.assertEqual(redir._operations, {})31 self.assertFalse(hasattr(self.text, 'widget'))32class WidgetRedirectorTest(unittest.TestCase):33 @classmethod34 def setUpClass(cls):35 requires('gui')36 cls.tk = Tk()37 cls.text = Text(cls.tk)38 @classmethod39 def tearDownClass(cls):40 cls.text.destroy()41 cls.tk.destroy()42 del cls.text, cls.tk43 def setUp(self):44 self.redir = WidgetRedirector(self.text)45 self.func = Func()46 self.orig_insert = self.redir.register('insert', self.func)47 self.text.insert('insert', 'asdf') # leaves self.text empty48 def tearDown(self):49 self.text.delete('1.0', 'end')50 self.redir.close()51 def test_repr(self): # partly for 100% coverage52 self.assertIn('Redirector', repr(self.redir))53 self.assertIn('Original', repr(self.orig_insert))54 def test_register(self):55 self.assertEqual(self.text.get('1.0', 'end'), '\n')56 self.assertEqual(self.func.args, ('insert', 'asdf'))57 self.assertIn('insert', self.redir._operations)58 self.assertIn('insert', self.text.__dict__)59 self.assertEqual(self.text.insert, self.func)60 def test_original_command(self):61 self.assertEqual(self.orig_insert.operation, 'insert')62 self.assertEqual(self.orig_insert.tk_call, self.text.tk.call)63 self.orig_insert('insert', 'asdf')64 self.assertEqual(self.text.get('1.0', 'end'), 'asdf\n')65 def test_unregister(self):66 self.assertIsNone(self.redir.unregister('invalid operation name'))67 self.assertEqual(self.redir.unregister('insert'), self.func)68 self.assertNotIn('insert', self.redir._operations)69 self.assertNotIn('insert', self.text.__dict__)70 def test_unregister_no_attribute(self):71 del self.text.insert72 self.assertEqual(self.redir.unregister('insert'), self.func)73 def test_dispatch_intercept(self):74 self.func.__init__(True)75 self.assertTrue(self.redir.dispatch('insert', False))76 self.assertFalse(self.func.args[0])77 def test_dispatch_bypass(self):78 self.orig_insert('insert', 'asdf')79 # tk.call returns '' where Python would return None80 self.assertEqual(self.redir.dispatch('delete', '1.0', 'end'), '')81 self.assertEqual(self.text.get('1.0', 'end'), '\n')82 def test_dispatch_error(self):83 self.func.__init__(TclError())84 self.assertEqual(self.redir.dispatch('insert', False), '')85 self.assertEqual(self.redir.dispatch('invalid'), '')86 def test_command_dispatch(self):87 # Test that .__init__ causes redirection of tk calls88 # through redir.dispatch89 self.tk.call(self.text._w, 'insert', 'hello')90 self.assertEqual(self.func.args, ('hello',))91 self.assertEqual(self.text.get('1.0', 'end'), '\n')92 # Ensure that called through redir .dispatch and not through93 # self.text.insert by having mock raise TclError.94 self.func.__init__(TclError())95 self.assertEqual(self.tk.call(self.text._w, 'insert', 'boo'), '')96if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful