diff -r 373ec8711ad0 Lib/idlelib/configHelpSourceEdit.py --- a/Lib/idlelib/configHelpSourceEdit.py Tue Feb 11 10:30:59 2014 +0200 +++ b/Lib/idlelib/configHelpSourceEdit.py Sun Feb 16 13:14:47 2014 -0500 @@ -159,11 +159,16 @@ self.destroy() if __name__ == '__main__': - #test the dialog + import unittest + unittest.main( + 'idlelib.idle_test.test_config_helpsource', verbosity=2, exit=False) + + # test the dialog root = Tk() def run(): keySeq = '' dlg = GetHelpSourceDialog(root, 'Get Help Source') print(dlg.result) - Button(root,text='Dialog', command=run).pack() + Button(root, text='Dialog', command=run).pack() root.mainloop() + diff -r 373ec8711ad0 Lib/idlelib/idle_test/mock_tk.py --- a/Lib/idlelib/idle_test/mock_tk.py Tue Feb 11 10:30:59 2014 +0200 +++ b/Lib/idlelib/idle_test/mock_tk.py Sun Feb 16 13:14:47 2014 -0500 @@ -1,8 +1,11 @@ """Classes that replace tkinter gui objects used by an object being tested. -A gui object is anything with a master or parent paramenter, which is typically -required in spite of what the doc strings say. +A gui object is anything with a master or parent paramenter, which is +typically required in spite of what the doc strings say. These classes +are typically used in dummy classes that allow tests of non-gui +functions and methods to run without a gui available. """ +from _tkinter import TclError class Var: "Use for String/Int/BooleanVar: incomplete" @@ -66,7 +69,10 @@ showinfo = Mbox_func() # None showwarning = Mbox_func() # None -from _tkinter import TclError +class Entry: + "Minimal mock for tkinter.Entry; expand as needed." + def focus_set(self): + pass class Text: """A semi-functional non-gui replacement for tkinter.Text text editors. diff -r 373ec8711ad0 Lib/idlelib/idle_test/test_config_helpsource.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/idlelib/idle_test/test_config_helpsource.py Sun Feb 16 13:14:47 2014 -0500 @@ -0,0 +1,103 @@ +"""Unit test for idlelib.configHelpSourceEdit""" +import unittest +from idlelib.idle_test.mock_tk import Var, Mbox, Entry +from idlelib import configHelpSourceEdit as helpsource_dialog_module + +helpsource_dialog = helpsource_dialog_module.GetHelpSourceDialog + +class dummy_helpsource_dialog: + # Mock for testing the following methods + MenuOk = helpsource_dialog.MenuOk + PathOk = helpsource_dialog.PathOk + Cancel = helpsource_dialog.Cancel + Ok = helpsource_dialog.Ok + # Attributes required by MenuOk and PathOk + entryMenu = Entry() + entryPath = Entry() + # Attributes, constant or variable required for tests. + menu = Var() + path = Var() + result = None + destroyed = False + + def destroy(self): + self.destroyed = True + +# menu_ok calls Mbox.showerror if menu is not ok +orig_mbox = helpsource_dialog_module.tkMessageBox +showerror = Mbox.showerror + + +class ConfigHelpSourceDialog(unittest.TestCase): + dialog = dummy_helpsource_dialog() + + @classmethod + def setUpClass(cls): + helpsource_dialog_module.tkMessageBox = Mbox + + @classmethod + def tearDownClass(cls): + helpsource_dialog_module.tkMessageBox = orig_mbox + + def test_blank_menu(self): + self.dialog.menu.set('') + self.assertEqual(self.dialog.MenuOk(), False) + self.assertEqual(showerror.title, 'Menu Item Error') + self.assertIn('No', showerror.message) + + def test_long_menu(self): + self.dialog.menu.set('foobar' * 8) + self.assertEqual(self.dialog.MenuOk(), False) + self.assertEqual(showerror.title, 'Menu Item Error') + self.assertIn('too long', showerror.message) + + def test_good_menu(self): + self.dialog.menu.set(' this name is good') + self.assertEqual(self.dialog.MenuOk(), True) + + def test_blank_path(self): + self.dialog.path.set('') + self.assertEqual(self.dialog.PathOk(), False) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('No', showerror.message) + + def test_invalid_paths(self): + # Test for invalid url path + self.dialog.path.set('ww.python.org') + self.assertEqual(self.dialog.PathOk(), False) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('not', showerror.message) + + # Test for invalid file path + self.dialog.path.set('testfile' * 8) + self.assertEqual(self.dialog.PathOk(), False) + self.assertEqual(showerror.title, 'File Path Error') + self.assertIn('not', showerror.message) + + def test_good_path(self): + # Test for valid url path + self.dialog.path.set('www.python.org') + self.assertEqual(self.dialog.PathOk(), True) + + # Test for valid file path + self.dialog.path.set('/') + self.assertEqual(self.dialog.PathOk(), True) + + def test_ok(self): + self.dialog.destroyed = False + self.dialog.menu.set('good') + self.dialog.path.set('www.python.org') + self.dialog.Ok() + self.assertEqual(self.dialog.result, ('good', 'www.python.org')) + self.assertTrue(self.dialog.destroyed) + + def test_cancel(self): + self.dialog.destroyed = False + self.dialog.menu.set('good') + self.dialog.path.set('www.python.org') + self.dialog.Cancel() + self.assertEqual(self.dialog.result, None) + self.assertTrue(self.dialog.destroyed) + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=False)