Skip to content

Commit 4a3f135

Browse files
committed
Issue #18910: test_textView - since all tests require 'gui', make root global.
Subclass TextViewer to add mock methods instead of monkey-patching it.
1 parent 780b585 commit 4a3f135

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed
+34-33
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,60 @@
1-
'''Test the functions and main class method of textView.py.'''
1+
'''Test the functions and main class method of textView.py.
2+
3+
Since all methods and functions create (or destroy) a TextViewer, which
4+
is a widget containing multiple widgets, all tests must be gui tests.
5+
Using mock Text would not change this. Other mocks are used to retrieve
6+
information about calls.
7+
8+
The coverage is essentially 100%.
9+
'''
10+
from test.support import requires
11+
requires('gui')
212

313
import unittest
414
import os
5-
from test.support import requires
615
from tkinter import Tk, Text, TclError
716
from idlelib import textView as tv
817
from idlelib.idle_test.mock_idle import Func
918
from idlelib.idle_test.mock_tk import Mbox
1019

11-
orig_mbox = tv.tkMessageBox
20+
def setUpModule():
21+
global root
22+
root = Tk()
1223

13-
class TextViewTest(unittest.TestCase):
24+
def tearDownModule():
25+
global root
26+
root.destroy()
27+
del root
1428

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()
2329

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+
class TV(tv.TextViewer): # used by TextViewTest
31+
transient = Func()
32+
grab_set = Func()
33+
wait_window = Func()
34+
35+
class TextViewTest(unittest.TestCase):
3036

3137
def setUp(self):
32-
TV = self.TV
3338
TV.transient.__init__()
3439
TV.grab_set.__init__()
3540
TV.wait_window.__init__()
3641

37-
3842
def test_init_modal(self):
39-
TV = self.TV
40-
view = TV(self.root, 'Title', 'test text')
43+
view = TV(root, 'Title', 'test text')
4144
self.assertTrue(TV.transient.called)
4245
self.assertTrue(TV.grab_set.called)
4346
self.assertTrue(TV.wait_window.called)
4447
view.Ok()
4548

4649
def test_init_nonmodal(self):
47-
TV = self.TV
48-
view = TV(self.root, 'Title', 'test text', modal=False)
50+
view = TV(root, 'Title', 'test text', modal=False)
4951
self.assertFalse(TV.transient.called)
5052
self.assertFalse(TV.grab_set.called)
5153
self.assertFalse(TV.wait_window.called)
5254
view.Ok()
5355

5456
def test_ok(self):
55-
view = self.TV(self.root, 'Title', 'test text', modal=False)
57+
view = TV(root, 'Title', 'test text', modal=False)
5658
view.destroy = Func()
5759
view.Ok()
5860
self.assertTrue(view.destroy.called)
@@ -64,33 +66,32 @@ class textviewTest(unittest.TestCase):
6466

6567
@classmethod
6668
def setUpClass(cls):
67-
requires('gui')
68-
cls.root = Tk()
69+
cls.orig_mbox = tv.tkMessageBox
6970
tv.tkMessageBox = Mbox
7071

7172
@classmethod
7273
def tearDownClass(cls):
73-
cls.root.destroy()
74-
del cls.root
75-
tv.tkMessageBox = orig_mbox
74+
tv.tkMessageBox = cls.orig_mbox
75+
del cls.orig_mbox
7676

7777
def test_view_text(self):
7878
# If modal True, tkinter will error with 'can't invoke "event" command'
79-
view = tv.view_text(self.root, 'Title', 'test text', modal=False)
79+
view = tv.view_text(root, 'Title', 'test text', modal=False)
8080
self.assertIsInstance(view, tv.TextViewer)
8181

8282
def test_view_file(self):
8383
test_dir = os.path.dirname(__file__)
8484
testfile = os.path.join(test_dir, 'test_textview.py')
85-
view = tv.view_file(self.root, 'Title', testfile, modal=False)
85+
view = tv.view_file(root, 'Title', testfile, modal=False)
8686
self.assertIsInstance(view, tv.TextViewer)
8787
self.assertIn('Test', view.textView.get('1.0', '1.end'))
8888
view.Ok()
8989

9090
# Mock messagebox will be used and view_file will not return anything
9191
testfile = os.path.join(test_dir, '../notthere.py')
92-
view = tv.view_file(self.root, 'Title', testfile, modal=False)
92+
view = tv.view_file(root, 'Title', testfile, modal=False)
9393
self.assertIsNone(view)
9494

95+
9596
if __name__ == '__main__':
9697
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)