diff -r e7ebf94629d2 Lib/idlelib/configHelpSourceEdit.py --- a/Lib/idlelib/configHelpSourceEdit.py Mon Jun 02 00:30:48 2014 +0100 +++ b/Lib/idlelib/configHelpSourceEdit.py Mon Jun 02 14:24:40 2014 +0530 @@ -23,10 +23,10 @@ self.title(title) self.transient(parent) self.grab_set() - self.protocol("WM_DELETE_WINDOW", self.Cancel) + self.protocol("WM_DELETE_WINDOW", self.cancel) self.parent = parent self.result = None - self.CreateWidgets() + self.create_widgets() self.menu.set(menuItem) self.path.set(filePath) self.withdraw() #hide while setting geometry @@ -41,10 +41,10 @@ ((parent.winfo_height()/2 - self.winfo_reqheight()/2) if not _htest else 150))) self.deiconify() #geometry set, unhide - self.bind('', self.Ok) + self.bind('', self.ok) self.wait_window() - def CreateWidgets(self): + def create_widgets(self): self.menu = StringVar(self) self.path = StringVar(self) self.fontSize = StringVar(self) @@ -65,18 +65,18 @@ labelPath.pack(anchor=W, padx=5, pady=3) self.entryPath.pack(anchor=W, padx=5, pady=3) browseButton = Button(self.frameMain, text='Browse', width=8, - command=self.browseFile) + command=self.browse_file) browseButton.pack(pady=3) frameButtons = Frame(self) frameButtons.pack(side=BOTTOM, fill=X) self.buttonOk = Button(frameButtons, text='OK', - width=8, default=ACTIVE, command=self.Ok) + width=8, default=ACTIVE, command=self.ok) self.buttonOk.grid(row=0, column=0, padx=5,pady=5) self.buttonCancel = Button(frameButtons, text='Cancel', - width=8, command=self.Cancel) + width=8, command=self.cancel) self.buttonCancel.grid(row=0, column=1, padx=5, pady=5) - def browseFile(self): + def browse_file(self): filetypes = [ ("HTML Files", "*.htm *.html", "TEXT"), ("PDF Files", "*.pdf", "TEXT"), @@ -99,9 +99,9 @@ if file: self.path.set(file) - def MenuOk(self): + def menu_ok(self): "Simple validity check for a sensible menu item name" - menuOk = True + menu_ok = True menu = self.menu.get() menu.strip() if not menu: @@ -109,19 +109,19 @@ message='No menu item specified', parent=self) self.entryMenu.focus_set() - menuOk = False + menu_ok = False elif len(menu) > 30: tkMessageBox.showerror(title='Menu Item Error', message='Menu item too long:' '\nLimit 30 characters.', parent=self) self.entryMenu.focus_set() - menuOk = False - return menuOk + menu_ok = False + return menu_ok - def PathOk(self): + def path_ok(self): "Simple validity check for menu file path" - pathOk = True + path_ok = True path = self.path.get() path.strip() if not path: #no path specified @@ -129,7 +129,7 @@ message='No help file path specified.', parent=self) self.entryPath.focus_set() - pathOk = False + path_ok = False elif path.startswith(('www.', 'http')): pass else: @@ -140,11 +140,11 @@ message='Help file path does not exist.', parent=self) self.entryPath.focus_set() - pathOk = False - return pathOk + path_ok = False + return path_ok - def Ok(self, event=None): - if self.MenuOk() and self.PathOk(): + def ok(self, event=None): + if self.menu_ok() and self.path_ok(): self.result = (self.menu.get().strip(), self.path.get().strip()) if sys.platform == 'darwin': @@ -157,10 +157,14 @@ self.result[1] = "file://" + path self.destroy() - def Cancel(self, event=None): + def cancel(self, event=None): self.result = None self.destroy() if __name__ == '__main__': + import unittest + unittest.main('idlelib.idle_test.test_config_help', + verbosity=2, exit=False) + from idlelib.idle_test.htest import run run(GetHelpSourceDialog) diff -r e7ebf94629d2 Lib/idlelib/idle_test/mock_tk.py --- a/Lib/idlelib/idle_test/mock_tk.py Mon Jun 02 00:30:48 2014 +0100 +++ b/Lib/idlelib/idle_test/mock_tk.py Mon Jun 02 14:24:40 2014 +0530 @@ -277,3 +277,8 @@ def bind(sequence=None, func=None, add=None): "Bind to this widget at event sequence a call to function func." pass + +class Entry: + "Mock for tkinter.Entry." + def focus_set(self): + pass diff -r e7ebf94629d2 Lib/idlelib/idle_test/test_config_help.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/idlelib/idle_test/test_config_help.py Mon Jun 02 14:24:40 2014 +0530 @@ -0,0 +1,106 @@ +"""Unittests for idlelib.configHelpSourceEdit""" +import unittest +from idlelib.idle_test.mock_tk import Var, Mbox, Entry +from idlelib import configHelpSourceEdit as help_dialog_module + +help_dialog = help_dialog_module.GetHelpSourceDialog + + +class Dummy_help_dialog: + # Mock for testing the following methods of help_dialog + menu_ok = help_dialog.menu_ok + path_ok = help_dialog.path_ok + ok = help_dialog.ok + cancel = help_dialog.cancel + # Attributes, constant or variable, needed for tests + menu = Var() + entryMenu = Entry() + path = Var() + entryPath = Entry() + result = None + destroyed = False + + def destroy(self): + self.destroyed = True + + +# menu_ok and path_ok call Mbox.showerror if menu and path are not ok. +orig_mbox = help_dialog_module.tkMessageBox +showerror = Mbox.showerror + + +class ConfigHelpTest(unittest.TestCase): + dialog = Dummy_help_dialog() + + @classmethod + def setUpClass(cls): + help_dialog_module.tkMessageBox = Mbox + + @classmethod + def tearDownClass(cls): + help_dialog_module.tkMessageBox = orig_mbox + + def test_blank_menu(self): + self.dialog.menu.set('') + self.assertFalse(self.dialog.menu_ok()) + self.assertEqual(showerror.title, 'Menu Item Error') + self.assertIn('No', showerror.message) + + def test_long_menu(self): + self.dialog.menu.set('hello' * 10) + self.assertFalse(self.dialog.menu_ok()) + self.assertEqual(showerror.title, 'Menu Item Error') + self.assertIn('long', showerror.message) + + def test_good_menu(self): + self.dialog.menu.set('help') + showerror.title = 'No Error' # should not be called + self.assertTrue(self.dialog.menu_ok()) + self.assertEqual(showerror.title, 'No Error') + + def test_blank_path(self): + self.dialog.path.set('') + self.assertFalse(self.dialog.path_ok()) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('No', showerror.message) + + def test_invalid_file_path(self): + self.dialog.path.set('foobar' * 100) + self.assertFalse(self.dialog.path_ok()) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('not exist', showerror.message) + + def test_invalid_url_path(self): + self.dialog.path.set('ww.foobar.com') + self.assertFalse(self.dialog.path_ok()) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('not exist', showerror.message) + + self.dialog.path.set('htt.foobar.com') + self.assertFalse(self.dialog.path_ok()) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('not exist', showerror.message) + + def test_good_path(self): + self.dialog.path.set('https://docs.python.org') + showerror.title = 'No Error' # should not be called + self.assertTrue(self.dialog.path_ok()) + self.assertEqual(showerror.title, 'No Error') + + def test_ok(self): + self.dialog.destroyed = False + self.dialog.menu.set('help') + self.dialog.path.set('https://docs.python.org') + self.dialog.ok() + self.assertEqual(self.dialog.result, ('help', + 'https://docs.python.org')) + self.assertTrue(self.dialog.destroyed) + + def test_cancel(self): + self.dialog.destroyed = False + self.dialog.cancel() + self.assertEqual(self.dialog.result, None) + self.assertTrue(self.dialog.destroyed) + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=False)