Skip to content

Commit c64d942

Browse files
committed
Issue #20640: Add tests for idlelib.configHelpSourceEdit.
Patch by Saimadhav Heblikar.
1 parent 507898d commit c64d942

File tree

3 files changed

+136
-21
lines changed

3 files changed

+136
-21
lines changed

Lib/idlelib/configHelpSourceEdit.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ def __init__(self, parent, title, menuItem='', filePath='', _htest=False):
2323
self.title(title)
2424
self.transient(parent)
2525
self.grab_set()
26-
self.protocol("WM_DELETE_WINDOW", self.Cancel)
26+
self.protocol("WM_DELETE_WINDOW", self.cancel)
2727
self.parent = parent
2828
self.result = None
29-
self.CreateWidgets()
29+
self.create_widgets()
3030
self.menu.set(menuItem)
3131
self.path.set(filePath)
3232
self.withdraw() #hide while setting geometry
@@ -41,10 +41,10 @@ def __init__(self, parent, title, menuItem='', filePath='', _htest=False):
4141
((parent.winfo_height()/2 - self.winfo_reqheight()/2)
4242
if not _htest else 150)))
4343
self.deiconify() #geometry set, unhide
44-
self.bind('<Return>', self.Ok)
44+
self.bind('<Return>', self.ok)
4545
self.wait_window()
4646

47-
def CreateWidgets(self):
47+
def create_widgets(self):
4848
self.menu = StringVar(self)
4949
self.path = StringVar(self)
5050
self.fontSize = StringVar(self)
@@ -65,18 +65,18 @@ def CreateWidgets(self):
6565
labelPath.pack(anchor=W, padx=5, pady=3)
6666
self.entryPath.pack(anchor=W, padx=5, pady=3)
6767
browseButton = Button(self.frameMain, text='Browse', width=8,
68-
command=self.browseFile)
68+
command=self.browse_file)
6969
browseButton.pack(pady=3)
7070
frameButtons = Frame(self)
7171
frameButtons.pack(side=BOTTOM, fill=X)
7272
self.buttonOk = Button(frameButtons, text='OK',
73-
width=8, default=ACTIVE, command=self.Ok)
73+
width=8, default=ACTIVE, command=self.ok)
7474
self.buttonOk.grid(row=0, column=0, padx=5,pady=5)
7575
self.buttonCancel = Button(frameButtons, text='Cancel',
76-
width=8, command=self.Cancel)
76+
width=8, command=self.cancel)
7777
self.buttonCancel.grid(row=0, column=1, padx=5, pady=5)
7878

79-
def browseFile(self):
79+
def browse_file(self):
8080
filetypes = [
8181
("HTML Files", "*.htm *.html", "TEXT"),
8282
("PDF Files", "*.pdf", "TEXT"),
@@ -99,37 +99,37 @@ def browseFile(self):
9999
if file:
100100
self.path.set(file)
101101

102-
def MenuOk(self):
102+
def menu_ok(self):
103103
"Simple validity check for a sensible menu item name"
104-
menuOk = True
104+
menu_ok = True
105105
menu = self.menu.get()
106106
menu.strip()
107107
if not menu:
108108
tkMessageBox.showerror(title='Menu Item Error',
109109
message='No menu item specified',
110110
parent=self)
111111
self.entryMenu.focus_set()
112-
menuOk = False
112+
menu_ok = False
113113
elif len(menu) > 30:
114114
tkMessageBox.showerror(title='Menu Item Error',
115115
message='Menu item too long:'
116116
'\nLimit 30 characters.',
117117
parent=self)
118118
self.entryMenu.focus_set()
119-
menuOk = False
120-
return menuOk
119+
menu_ok = False
120+
return menu_ok
121121

122-
def PathOk(self):
122+
def path_ok(self):
123123
"Simple validity check for menu file path"
124-
pathOk = True
124+
path_ok = True
125125
path = self.path.get()
126126
path.strip()
127127
if not path: #no path specified
128128
tkMessageBox.showerror(title='File Path Error',
129129
message='No help file path specified.',
130130
parent=self)
131131
self.entryPath.focus_set()
132-
pathOk = False
132+
path_ok = False
133133
elif path.startswith(('www.', 'http')):
134134
pass
135135
else:
@@ -140,11 +140,11 @@ def PathOk(self):
140140
message='Help file path does not exist.',
141141
parent=self)
142142
self.entryPath.focus_set()
143-
pathOk = False
144-
return pathOk
143+
path_ok = False
144+
return path_ok
145145

146-
def Ok(self, event=None):
147-
if self.MenuOk() and self.PathOk():
146+
def ok(self, event=None):
147+
if self.menu_ok() and self.path_ok():
148148
self.result = (self.menu.get().strip(),
149149
self.path.get().strip())
150150
if sys.platform == 'darwin':
@@ -157,10 +157,14 @@ def Ok(self, event=None):
157157
self.result[1] = "file://" + path
158158
self.destroy()
159159

160-
def Cancel(self, event=None):
160+
def cancel(self, event=None):
161161
self.result = None
162162
self.destroy()
163163

164164
if __name__ == '__main__':
165+
import unittest
166+
unittest.main('idlelib.idle_test.test_config_help',
167+
verbosity=2, exit=False)
168+
165169
from idlelib.idle_test.htest import run
166170
run(GetHelpSourceDialog)

Lib/idlelib/idle_test/mock_tk.py

+5
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,8 @@ def see(self, index):
296296
def bind(sequence=None, func=None, add=None):
297297
"Bind to this widget at event sequence a call to function func."
298298
pass
299+
300+
class Entry:
301+
"Mock for tkinter.Entry."
302+
def focus_set(self):
303+
pass
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""Unittests for idlelib.configHelpSourceEdit"""
2+
import unittest
3+
from idlelib.idle_test.mock_tk import Var, Mbox, Entry
4+
from idlelib import configHelpSourceEdit as help_dialog_module
5+
6+
help_dialog = help_dialog_module.GetHelpSourceDialog
7+
8+
9+
class Dummy_help_dialog:
10+
# Mock for testing the following methods of help_dialog
11+
menu_ok = help_dialog.menu_ok
12+
path_ok = help_dialog.path_ok
13+
ok = help_dialog.ok
14+
cancel = help_dialog.cancel
15+
# Attributes, constant or variable, needed for tests
16+
menu = Var()
17+
entryMenu = Entry()
18+
path = Var()
19+
entryPath = Entry()
20+
result = None
21+
destroyed = False
22+
23+
def destroy(self):
24+
self.destroyed = True
25+
26+
27+
# menu_ok and path_ok call Mbox.showerror if menu and path are not ok.
28+
orig_mbox = help_dialog_module.tkMessageBox
29+
showerror = Mbox.showerror
30+
31+
32+
class ConfigHelpTest(unittest.TestCase):
33+
dialog = Dummy_help_dialog()
34+
35+
@classmethod
36+
def setUpClass(cls):
37+
help_dialog_module.tkMessageBox = Mbox
38+
39+
@classmethod
40+
def tearDownClass(cls):
41+
help_dialog_module.tkMessageBox = orig_mbox
42+
43+
def test_blank_menu(self):
44+
self.dialog.menu.set('')
45+
self.assertFalse(self.dialog.menu_ok())
46+
self.assertEqual(showerror.title, 'Menu Item Error')
47+
self.assertIn('No', showerror.message)
48+
49+
def test_long_menu(self):
50+
self.dialog.menu.set('hello' * 10)
51+
self.assertFalse(self.dialog.menu_ok())
52+
self.assertEqual(showerror.title, 'Menu Item Error')
53+
self.assertIn('long', showerror.message)
54+
55+
def test_good_menu(self):
56+
self.dialog.menu.set('help')
57+
showerror.title = 'No Error' # should not be called
58+
self.assertTrue(self.dialog.menu_ok())
59+
self.assertEqual(showerror.title, 'No Error')
60+
61+
def test_blank_path(self):
62+
self.dialog.path.set('')
63+
self.assertFalse(self.dialog.path_ok())
64+
self.assertEqual(showerror.title, 'File Path Error')
65+
self.assertIn('No', showerror.message)
66+
67+
def test_invalid_file_path(self):
68+
self.dialog.path.set('foobar' * 100)
69+
self.assertFalse(self.dialog.path_ok())
70+
self.assertEqual(showerror.title, 'File Path Error')
71+
self.assertIn('not exist', showerror.message)
72+
73+
def test_invalid_url_path(self):
74+
self.dialog.path.set('ww.foobar.com')
75+
self.assertFalse(self.dialog.path_ok())
76+
self.assertEqual(showerror.title, 'File Path Error')
77+
self.assertIn('not exist', showerror.message)
78+
79+
self.dialog.path.set('htt.foobar.com')
80+
self.assertFalse(self.dialog.path_ok())
81+
self.assertEqual(showerror.title, 'File Path Error')
82+
self.assertIn('not exist', showerror.message)
83+
84+
def test_good_path(self):
85+
self.dialog.path.set('https://docs.python.org')
86+
showerror.title = 'No Error' # should not be called
87+
self.assertTrue(self.dialog.path_ok())
88+
self.assertEqual(showerror.title, 'No Error')
89+
90+
def test_ok(self):
91+
self.dialog.destroyed = False
92+
self.dialog.menu.set('help')
93+
self.dialog.path.set('https://docs.python.org')
94+
self.dialog.ok()
95+
self.assertEqual(self.dialog.result, ('help',
96+
'https://docs.python.org'))
97+
self.assertTrue(self.dialog.destroyed)
98+
99+
def test_cancel(self):
100+
self.dialog.destroyed = False
101+
self.dialog.cancel()
102+
self.assertEqual(self.dialog.result, None)
103+
self.assertTrue(self.dialog.destroyed)
104+
105+
if __name__ == '__main__':
106+
unittest.main(verbosity=2, exit=False)

0 commit comments

Comments
 (0)