[3.6] bpo-30495: IDLE: improve textview with docstrings, PEP8 names, more tests. (GH-2283) (#2496)

Split TextViewer class into ViewWindow, ViewFrame, and TextFrame classes so that instances
of the latter two can be placed with other widgets within a multiframe window.
Patch by Cheryl Sabella.
(cherry picked from commit 42bc8be)
This commit is contained in:
terryjreedy 2017-06-29 19:15:18 -04:00 committed by GitHub
parent 1d56ed5210
commit 6f31717c47
6 changed files with 141 additions and 100 deletions

View file

@ -1,7 +1,7 @@
'''Test idlelib.textview.
Since all methods and functions create (or destroy) a TextViewer, which
is a widget containing multiple widgets, all tests must be gui tests.
Since all methods and functions create (or destroy) a ViewWindow, which
is a widget containing a widget, etcetera, all tests must be gui tests.
Using mock Text would not change this. Other mocks are used to retrieve
information about calls.
@ -13,7 +13,8 @@ requires('gui')
import unittest
import os
from tkinter import Tk, Button
from tkinter import Tk
from tkinter.ttk import Button
from idlelib.idle_test.mock_idle import Func
from idlelib.idle_test.mock_tk import Mbox_func
@ -25,44 +26,44 @@ def setUpModule():
def tearDownModule():
global root
root.update_idletasks()
root.destroy() # Pyflakes falsely sees root as undefined.
root.destroy()
del root
# If we call TextViewer or wrapper functions with defaults
# If we call ViewWindow or wrapper functions with defaults
# modal=True, _utest=False, test hangs on call to wait_window.
# Have also gotten tk error 'can't invoke "event" command'.
class TV(tv.TextViewer): # Used in TextViewTest.
class VW(tv.ViewWindow): # Used in ViewWindowTest.
transient = Func()
grab_set = Func()
wait_window = Func()
# Call wrapper class with mock wait_window.
class TextViewTest(unittest.TestCase):
# Call wrapper class VW with mock wait_window.
class ViewWindowTest(unittest.TestCase):
def setUp(self):
TV.transient.__init__()
TV.grab_set.__init__()
TV.wait_window.__init__()
VW.transient.__init__()
VW.grab_set.__init__()
VW.wait_window.__init__()
def test_init_modal(self):
view = TV(root, 'Title', 'test text')
self.assertTrue(TV.transient.called)
self.assertTrue(TV.grab_set.called)
self.assertTrue(TV.wait_window.called)
view = VW(root, 'Title', 'test text')
self.assertTrue(VW.transient.called)
self.assertTrue(VW.grab_set.called)
self.assertTrue(VW.wait_window.called)
view.ok()
def test_init_nonmodal(self):
view = TV(root, 'Title', 'test text', modal=False)
self.assertFalse(TV.transient.called)
self.assertFalse(TV.grab_set.called)
self.assertFalse(TV.wait_window.called)
view = VW(root, 'Title', 'test text', modal=False)
self.assertFalse(VW.transient.called)
self.assertFalse(VW.grab_set.called)
self.assertFalse(VW.wait_window.called)
view.ok()
def test_ok(self):
view = TV(root, 'Title', 'test text', modal=False)
view = VW(root, 'Title', 'test text', modal=False)
view.destroy = Func()
view.ok()
self.assertTrue(view.destroy.called)
@ -70,7 +71,28 @@ class TextViewTest(unittest.TestCase):
view.destroy()
# Call TextViewer with modal=False.
class TextFrameTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
"By itself, this tests that file parsed without exception."
cls.root = root = Tk()
root.withdraw()
cls.frame = tv.TextFrame(root, 'test text')
@classmethod
def tearDownClass(cls):
del cls.frame
cls.root.update_idletasks()
cls.root.destroy()
del cls.root
def test_line1(self):
get = self.frame.text.get
self.assertEqual(get('1.0', '1.end'), 'test text')
# Call ViewWindow with modal=False.
class ViewFunctionTest(unittest.TestCase):
@classmethod
@ -85,13 +107,16 @@ class ViewFunctionTest(unittest.TestCase):
def test_view_text(self):
view = tv.view_text(root, 'Title', 'test text', modal=False)
self.assertIsInstance(view, tv.TextViewer)
self.assertIsInstance(view, tv.ViewWindow)
self.assertIsInstance(view.viewframe, tv.ViewFrame)
view.ok()
def test_view_file(self):
view = tv.view_file(root, 'Title', __file__, modal=False)
self.assertIsInstance(view, tv.TextViewer)
self.assertIn('Test', view.text.get('1.0', '1.end'))
self.assertIsInstance(view, tv.ViewWindow)
self.assertIsInstance(view.viewframe, tv.ViewFrame)
get = view.viewframe.textframe.text.get
self.assertIn('Test', get('1.0', '1.end'))
view.ok()
def test_bad_file(self):
@ -109,8 +134,7 @@ class ViewFunctionTest(unittest.TestCase):
self.assertEqual(tv.showerror.title, 'Unicode Decode Error')
# Call TextViewer with _utest=True.
# Call ViewWindow with _utest=True.
class ButtonClickTest(unittest.TestCase):
def setUp(self):
@ -131,7 +155,8 @@ class ButtonClickTest(unittest.TestCase):
self.assertEqual(self.called, True)
self.assertEqual(self.view.title(), 'TITLE_TEXT')
self.assertEqual(self.view.text.get('1.0', '1.end'), 'COMMAND')
self.assertEqual(self.view.viewframe.textframe.text.get('1.0', '1.end'),
'COMMAND')
def test_view_file_bind_with_button(self):
def _command():
@ -143,12 +168,11 @@ class ButtonClickTest(unittest.TestCase):
self.assertEqual(self.called, True)
self.assertEqual(self.view.title(), 'TITLE_FILE')
get = self.view.viewframe.textframe.text.get
with open(__file__) as f:
self.assertEqual(self.view.text.get('1.0', '1.end'),
f.readline().strip())
self.assertEqual(get('1.0', '1.end'), f.readline().strip())
f.readline()
self.assertEqual(self.view.text.get('3.0', '3.end'),
f.readline().strip())
self.assertEqual(get('3.0', '3.end'), f.readline().strip())
if __name__ == '__main__':