Skip to content

[3.6] bpo-30303: IDLE test_textview: add comments and test, increase coverage to… #1866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions Lib/idlelib/idle_test/test_textview.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Using mock Text would not change this. Other mocks are used to retrieve
information about calls.

Coverage: 94%.
Coverage: 100%.
'''
from idlelib import textview as tv
from test.support import requires
Expand All @@ -28,12 +28,18 @@ def tearDownModule():
root.destroy() # Pyflakes falsely sees root as undefined.
del root

# If we call TextViewer 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.
transient = Func()
grab_set = Func()
wait_window = Func()


# Call wrapper class with mock wait_window.
class TextViewTest(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -64,6 +70,7 @@ def test_ok(self):
view.destroy()


# Call TextViewer with modal=False.
class ViewFunctionTest(unittest.TestCase):

@classmethod
Expand All @@ -77,23 +84,71 @@ def tearDownClass(cls):
del cls.orig_error

def test_view_text(self):
# If modal True, get tk error 'can't invoke "event" command'.
view = tv.view_text(root, 'Title', 'test text', modal=False)
self.assertIsInstance(view, tv.TextViewer)
view.Ok()

def test_view_file(self):
test_dir = os.path.dirname(__file__)
testfile = os.path.join(test_dir, 'test_textview.py')
view = tv.view_file(root, 'Title', testfile, modal=False)
view = tv.view_file(root, 'Title', __file__, modal=False)
self.assertIsInstance(view, tv.TextViewer)
self.assertIn('Test', view.textView.get('1.0', '1.end'))
view.Ok()

def test_bad_file(self):
# Mock showerror will be used; view_file will return None.
testfile = os.path.join(test_dir, '../notthere.py')
view = tv.view_file(root, 'Title', testfile, modal=False)
view = tv.view_file(root, 'Title', 'abc.xyz', modal=False)
self.assertIsNone(view)
self.assertEqual(tv.showerror.title, 'File Load Error')

def test_bad_encoding(self):
p = os.path
fn = p.abspath(p.join(p.dirname(__file__), '..', 'CREDITS.txt'))
tv.showerror.title = None
view = tv.view_file(root, 'Title', fn, 'ascii', modal=False)
self.assertIsNone(view)
self.assertEqual(tv.showerror.title, 'Unicode Decode Error')



# Call TextViewer with _utest=True.
class ButtonClickTest(unittest.TestCase):

def setUp(self):
self.view = None
self.called = False

def tearDown(self):
if self.view:
self.view.destroy()

def test_view_text_bind_with_button(self):
def _command():
self.called = True
self.view = tv.view_text(root, 'TITLE_TEXT', 'COMMAND', _utest=True)
button = Button(root, text='BUTTON', command=_command)
button.invoke()
self.addCleanup(button.destroy)

self.assertEqual(self.called, True)
self.assertEqual(self.view.title(), 'TITLE_TEXT')
self.assertEqual(self.view.textView.get('1.0', '1.end'), 'COMMAND')

def test_view_file_bind_with_button(self):
def _command():
self.called = True
self.view = tv.view_file(root, 'TITLE_FILE', __file__, _utest=True)
button = Button(root, text='BUTTON', command=_command)
button.invoke()
self.addCleanup(button.destroy)

self.assertEqual(self.called, True)
self.assertEqual(self.view.title(), 'TITLE_FILE')
with open(__file__) as f:
self.assertEqual(self.view.textView.get('1.0', '1.end'),
f.readline().strip())
f.readline()
self.assertEqual(self.view.textView.get('3.0', '3.end'),
f.readline().strip())


if __name__ == '__main__':
Expand Down
2 changes: 2 additions & 0 deletions Lib/idlelib/textview.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def view_file(parent, title, filename, encoding=None, modal=True):
parent=parent)
else:
return view_text(parent, title, contents, modal)
return None


if __name__ == '__main__':
import unittest
Expand Down