Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 537e2c8

Browse files
committed
Issue #18910: Add unittest for textView. Patch by Phil Webster.
1 parent 0d3e4b4 commit 537e2c8

4 files changed

Lines changed: 115 additions & 4 deletions

File tree

Lib/idlelib/idle_test/mock_idle.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
class Func:
99
'''Mock function captures args and returns result set by test.
1010
11+
Attributes:
12+
self.called - records call even if no args, kwds passed.
13+
self.result - set by init, returned by call.
14+
self.args - captures positional arguments.
15+
self.kwds - captures keyword arguments.
16+
1117
Most common use will probably be to mock methods.
12-
mock_tk.Var and Mbox_func are special cases of this.
18+
Mock_tk.Var and Mbox_func are special variants of this.
1319
'''
1420
def __init__(self, result=None):
21+
self.called = False
1522
self.result = result
1623
self.args = None
1724
self.kwds = None
1825
def __call__(self, *args, **kwds):
26+
self.called = True
1927
self.args = args
2028
self.kwds = kwds
2129
return self.result
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'''Test the functions and main class method of textView.py.'''
2+
3+
import unittest
4+
import os
5+
from test.support import requires
6+
from tkinter import Tk, Text, TclError
7+
from idlelib import textView as tv
8+
from idlelib.idle_test.mock_idle import Func
9+
from idlelib.idle_test.mock_tk import Mbox
10+
11+
orig_mbox = tv.tkMessageBox
12+
13+
class TextViewTest(unittest.TestCase):
14+
15+
@classmethod
16+
def setUpClass(cls):
17+
requires('gui')
18+
cls.root = Tk()
19+
cls.TV = TV = tv.TextViewer
20+
TV.transient = Func()
21+
TV.grab_set = Func()
22+
TV.wait_window = Func()
23+
24+
@classmethod
25+
def tearDownClass(cls):
26+
cls.root.destroy()
27+
TV = cls.TV
28+
del cls.root, cls.TV
29+
del TV.transient, TV.grab_set, TV.wait_window
30+
31+
def setUp(self):
32+
TV = self.TV
33+
TV.transient.__init__()
34+
TV.grab_set.__init__()
35+
TV.wait_window.__init__()
36+
37+
38+
def test_init_modal(self):
39+
TV = self.TV
40+
view = TV(self.root, 'Title', 'test text')
41+
self.assertTrue(TV.transient.called)
42+
self.assertTrue(TV.grab_set.called)
43+
self.assertTrue(TV.wait_window.called)
44+
view.Ok()
45+
46+
def test_init_nonmodal(self):
47+
TV = self.TV
48+
view = TV(self.root, 'Title', 'test text', modal=False)
49+
self.assertFalse(TV.transient.called)
50+
self.assertFalse(TV.grab_set.called)
51+
self.assertFalse(TV.wait_window.called)
52+
view.Ok()
53+
54+
def test_ok(self):
55+
view = self.TV(self.root, 'Title', 'test text', modal=False)
56+
view.destroy = Func()
57+
view.Ok()
58+
self.assertTrue(view.destroy.called)
59+
del view.destroy # unmask real function
60+
view.destroy
61+
62+
63+
class textviewTest(unittest.TestCase):
64+
65+
@classmethod
66+
def setUpClass(cls):
67+
requires('gui')
68+
cls.root = Tk()
69+
tv.tkMessageBox = Mbox
70+
71+
@classmethod
72+
def tearDownClass(cls):
73+
cls.root.destroy()
74+
del cls.root
75+
tv.tkMessageBox = orig_mbox
76+
77+
def test_view_text(self):
78+
# If modal True, tkinter will error with 'can't invoke "event" command'
79+
view = tv.view_text(self.root, 'Title', 'test text', modal=False)
80+
self.assertIsInstance(view, tv.TextViewer)
81+
82+
def test_view_file(self):
83+
test_dir = os.path.dirname(__file__)
84+
testfile = os.path.join(test_dir, 'test_textview.py')
85+
view = tv.view_file(self.root, 'Title', testfile, modal=False)
86+
self.assertIsInstance(view, tv.TextViewer)
87+
self.assertIn('Test', view.textView.get('1.0', '1.end'))
88+
view.Ok()
89+
90+
# Mock messagebox will be used and view_file will not return anything
91+
testfile = os.path.join(test_dir, '../notthere.py')
92+
view = tv.view_file(self.root, 'Title', testfile, modal=False)
93+
self.assertIsNone(view)
94+
95+
if __name__ == '__main__':
96+
unittest.main(verbosity=2)

Lib/idlelib/textView.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ class TextViewer(Toplevel):
1212
def __init__(self, parent, title, text, modal=True, _htest=False):
1313
"""Show the given text in a scrollable window with a 'close' button
1414
15-
_htest - bool, change box location when running htest
15+
If modal option set to False, user can interact with other windows,
16+
otherwise they will be unable to interact with other windows until
17+
the textview window is closed.
18+
19+
_htest - bool; change box location when running htest.
1620
"""
1721
Toplevel.__init__(self, parent)
1822
self.configure(borderwidth=5)
@@ -68,14 +72,15 @@ def view_file(parent, title, filename, encoding=None, modal=True):
6872
try:
6973
with open(filename, 'r', encoding=encoding) as file:
7074
contents = file.read()
71-
except OSError:
72-
import tkinter.messagebox as tkMessageBox
75+
except IOError:
7376
tkMessageBox.showerror(title='File Load Error',
7477
message='Unable to load file %r .' % filename,
7578
parent=parent)
7679
else:
7780
return view_text(parent, title, contents, modal)
7881

7982
if __name__ == '__main__':
83+
import unittest
84+
unittest.main('idlelib.idle_test.test_textview', verbosity=2, exit=False)
8085
from idlelib.idle_test.htest import run
8186
run(TextViewer)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Build
6666
IDLE
6767
----
6868

69+
- Issue #18910: Add unittest for textView. Patch by Phil Webster.
70+
6971
- Issue #18292: Add unittest for AutoExpand. Patch by Saihadhav Heblikar.
7072

7173
- Issue #18409: Add unittest for AutoComplete. Patch by Phil Webster.

0 commit comments

Comments
 (0)