Issue #27714: For IDLE's test_textview, backport 3.x subclass with mocks

instead of overriding methods with mocks in original class and module.
This makes the 2.7 test_textview nearly identical to the 3.5/.6 test.
This commit is contained in:
Terry Jan Reedy 2016-08-10 19:41:39 -04:00
parent 5c715b0897
commit 0f0ead5c9a
1 changed files with 11 additions and 13 deletions

View File

@ -8,7 +8,11 @@ from idlelib import textView as tv
from idlelib.idle_test.mock_idle import Func from idlelib.idle_test.mock_idle import Func
from idlelib.idle_test.mock_tk import Mbox from idlelib.idle_test.mock_tk import Mbox
orig_mbox = tv.tkMessageBox
class TV(tv.TextViewer): # used by TextViewTest
transient = Func()
grab_set = Func()
wait_window = Func()
class textviewClassTest(unittest.TestCase): class textviewClassTest(unittest.TestCase):
@ -16,26 +20,18 @@ class textviewClassTest(unittest.TestCase):
def setUpClass(cls): def setUpClass(cls):
requires('gui') requires('gui')
cls.root = Tk() cls.root = Tk()
cls.TV = TV = tv.TextViewer
TV.transient = Func()
TV.grab_set = Func()
TV.wait_window = Func()
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
del cls.TV
cls.root.destroy() cls.root.destroy()
del cls.root del cls.root
def setUp(self): def setUp(self):
TV = self.TV
TV.transient.__init__() TV.transient.__init__()
TV.grab_set.__init__() TV.grab_set.__init__()
TV.wait_window.__init__() TV.wait_window.__init__()
def test_init_modal(self): def test_init_modal(self):
TV = self.TV
view = TV(self.root, 'Title', 'test text') view = TV(self.root, 'Title', 'test text')
self.assertTrue(TV.transient.called) self.assertTrue(TV.transient.called)
self.assertTrue(TV.grab_set.called) self.assertTrue(TV.grab_set.called)
@ -43,7 +39,6 @@ class textviewClassTest(unittest.TestCase):
view.Ok() view.Ok()
def test_init_nonmodal(self): def test_init_nonmodal(self):
TV = self.TV
view = TV(self.root, 'Title', 'test text', modal=False) view = TV(self.root, 'Title', 'test text', modal=False)
self.assertFalse(TV.transient.called) self.assertFalse(TV.transient.called)
self.assertFalse(TV.grab_set.called) self.assertFalse(TV.grab_set.called)
@ -51,7 +46,7 @@ class textviewClassTest(unittest.TestCase):
view.Ok() view.Ok()
def test_ok(self): def test_ok(self):
view = self.TV(self.root, 'Title', 'test text', modal=False) view = TV(self.root, 'Title', 'test text', modal=False)
view.destroy = Func() view.destroy = Func()
view.Ok() view.Ok()
self.assertTrue(view.destroy.called) self.assertTrue(view.destroy.called)
@ -59,19 +54,21 @@ class textviewClassTest(unittest.TestCase):
view.destroy view.destroy
class textviewTest(unittest.TestCase): class ViewFunctionTest(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
requires('gui') requires('gui')
cls.root = Tk() cls.root = Tk()
cls.orig_mbox = tv.tkMessageBox
tv.tkMessageBox = Mbox tv.tkMessageBox = Mbox
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
cls.root.destroy() cls.root.destroy()
del cls.root del cls.root
tv.tkMessageBox = orig_mbox tv.tkMessageBox = cls.orig_mbox
del cls.orig_mbox
def test_view_text(self): def test_view_text(self):
# If modal True, tkinter will error with 'can't invoke "event" command' # If modal True, tkinter will error with 'can't invoke "event" command'
@ -91,5 +88,6 @@ class textviewTest(unittest.TestCase):
view = tv.view_file(self.root, 'Title', testfile, modal=False) view = tv.view_file(self.root, 'Title', testfile, modal=False)
self.assertIsNone(view) self.assertIsNone(view)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main(verbosity=2) unittest.main(verbosity=2)