Issue #27380: IDLE: add query.HelpSource class and tests.

Remove modules that are combined in new module.
This commit is contained in:
Terry Jan Reedy 2016-07-08 00:22:50 -04:00
parent d6402a40d3
commit 8b22c0aada
6 changed files with 341 additions and 367 deletions

View file

@ -137,18 +137,6 @@ _editor_window_spec = {
"Best to close editor first."
}
GetHelpSourceDialog_spec = {
'file': 'config_help',
'kwds': {'title': 'Get helpsource',
'_htest': True},
'msg': "Enter menu item name and help file path\n "
"<nothing> and more than 30 chars are invalid menu item names.\n"
"<nothing>, file does not exist are invalid path items.\n"
"Test for incomplete web address for help file path.\n"
"A valid entry will be printed to shell with [0k].\n"
"[Cancel] will print None to shell",
}
# Update once issue21519 is resolved.
GetKeysDialog_spec = {
'file': 'config_key',
@ -175,6 +163,22 @@ _grep_dialog_spec = {
"should open that file \nin a new EditorWindow."
}
HelpSource_spec = {
'file': 'query',
'kwds': {'title': 'Help name and source',
'menuitem': 'test',
'filepath': __file__,
'used_names': {'abc'},
'_htest': True},
'msg': "Enter menu item name and help file path\n"
"'', > than 30 chars, and 'abc' are invalid menu item names.\n"
"'' and file does not exist are invalid path items.\n"
"Any url ('www...', 'http...') is accepted.\n"
"Test Browse with and without path, as cannot unittest.\n"
"A valid entry will be printed to shell with [0k]\n"
"or <return>. [Cancel] will print None to shell"
}
_io_binding_spec = {
'file': 'iomenu',
'kwds': {},
@ -241,7 +245,7 @@ Query_spec = {
'_htest': True},
'msg': "Enter with <Return> or [Ok]. Print valid entry to Shell\n"
"Blank line, after stripping, is ignored\n"
"Close dialog with valid entry, [Cancel] or [X]",
"Close dialog with valid entry, [Cancel] or [X]"
}

View file

@ -1,106 +0,0 @@
"""Unittests for idlelib.config_help.py"""
import unittest
from idlelib.idle_test.mock_tk import Var, Mbox, Entry
from idlelib import config_help 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)

View file

@ -1,6 +1,16 @@
"""Test idlelib.query.
Coverage: 100%.
Non-gui tests for Query, SectionName, ModuleName, and HelpSource use
dummy versions that extract the non-gui methods and add other needed
attributes. GUI tests create an instance of each class and simulate
entries and button clicks. Subclass tests only target the new code in
the subclass definition.
The appearance of the widgets is checked by the Query and
HelpSource htests. These are run by running query.py.
Coverage: 94% (100% for Query and SectionName).
6 of 8 missing are ModuleName exceptions I don't know how to trigger.
"""
from test.support import requires
from tkinter import Tk
@ -9,21 +19,9 @@ from unittest import mock
from idlelib.idle_test.mock_tk import Var, Mbox_func
from idlelib import query
Query = query.Query
class Dummy_Query:
# Mock for testing the following methods Query
entry_ok = Query.entry_ok
ok = Query.ok
cancel = Query.cancel
# Attributes, constant or variable, needed for tests
entry = Var()
result = None
destroyed = False
def destroy(self):
self.destroyed = True
# Mock entry.showerror messagebox so don't need click to continue
# when entry_ok and path_ok methods call it to display errors.
# entry_ok calls modal messagebox.showerror if entry is not ok.
# Mock showerrer so don't need to click to continue.
orig_showerror = query.showerror
showerror = Mbox_func() # Instance has __call__ method.
@ -34,7 +32,23 @@ def tearDownModule():
query.showerror = orig_showerror
# NON-GUI TESTS
class QueryTest(unittest.TestCase):
"Test Query base class."
class Dummy_Query:
# Test the following Query methods.
entry_ok = query.Query.entry_ok
ok = query.Query.ok
cancel = query.Query.cancel
# Add attributes needed for the tests.
entry = Var()
result = None
destroyed = False
def destroy(self):
self.destroyed = True
dialog = Dummy_Query()
def setUp(self):
@ -42,7 +56,7 @@ class QueryTest(unittest.TestCase):
self.dialog.result = None
self.dialog.destroyed = False
def test_blank_entry(self):
def test_entry_ok_blank(self):
dialog = self.dialog
Equal = self.assertEqual
dialog.entry.set(' ')
@ -51,7 +65,7 @@ class QueryTest(unittest.TestCase):
Equal(showerror.title, 'Entry Error')
self.assertIn('Blank', showerror.message)
def test_good_entry(self):
def test_entry_ok_good(self):
dialog = self.dialog
Equal = self.assertEqual
dialog.entry.set(' good ')
@ -59,7 +73,17 @@ class QueryTest(unittest.TestCase):
Equal((dialog.result, dialog.destroyed), (None, False))
Equal(showerror.title, None)
def test_ok(self):
def test_ok_blank(self):
dialog = self.dialog
Equal = self.assertEqual
dialog.entry.set('')
dialog.entry.focus_set = mock.Mock()
Equal(dialog.ok(), None)
self.assertTrue(dialog.entry.focus_set.called)
del dialog.entry.focus_set
Equal((dialog.result, dialog.destroyed), (None, False))
def test_ok_good(self):
dialog = self.dialog
Equal = self.assertEqual
dialog.entry.set('good')
@ -73,12 +97,14 @@ class QueryTest(unittest.TestCase):
Equal((dialog.result, dialog.destroyed), (None, True))
class Dummy_SectionName:
entry_ok = query.SectionName.entry_ok # Test override.
used_names = ['used']
entry = Var()
class SectionNameTest(unittest.TestCase):
"Test SectionName subclass of Query."
class Dummy_SectionName:
entry_ok = query.SectionName.entry_ok # Function being tested.
used_names = ['used']
entry = Var()
dialog = Dummy_SectionName()
def setUp(self):
@ -116,12 +142,14 @@ class SectionNameTest(unittest.TestCase):
Equal(showerror.title, None)
class Dummy_ModuleName:
entry_ok = query.ModuleName.entry_ok # Test override
text0 = ''
entry = Var()
class ModuleNameTest(unittest.TestCase):
"Test ModuleName subclass of Query."
class Dummy_ModuleName:
entry_ok = query.ModuleName.entry_ok # Funtion being tested.
text0 = ''
entry = Var()
dialog = Dummy_ModuleName()
def setUp(self):
@ -159,13 +187,119 @@ class ModuleNameTest(unittest.TestCase):
Equal(showerror.title, None)
# 3 HelpSource test classes each test one function.
orig_platform = query.platform
class HelpsourceBrowsefileTest(unittest.TestCase):
"Test browse_file method of ModuleName subclass of Query."
class Dummy_HelpSource:
browse_file = query.HelpSource.browse_file
pathvar = Var()
dialog = Dummy_HelpSource()
def test_file_replaces_path(self):
# Path is widget entry, file is file dialog return.
dialog = self.dialog
for path, func, result in (
# We need all combination to test all (most) code paths.
('', lambda a,b,c:'', ''),
('', lambda a,b,c: __file__, __file__),
('htest', lambda a,b,c:'', 'htest'),
('htest', lambda a,b,c: __file__, __file__)):
with self.subTest():
dialog.pathvar.set(path)
dialog.askfilename = func
dialog.browse_file()
self.assertEqual(dialog.pathvar.get(), result)
class HelpsourcePathokTest(unittest.TestCase):
"Test path_ok method of ModuleName subclass of Query."
class Dummy_HelpSource:
path_ok = query.HelpSource.path_ok
path = Var()
dialog = Dummy_HelpSource()
@classmethod
def tearDownClass(cls):
query.platform = orig_platform
def setUp(self):
showerror.title = None
def test_path_ok_blank(self):
dialog = self.dialog
Equal = self.assertEqual
dialog.path.set(' ')
Equal(dialog.path_ok(), None)
Equal(showerror.title, 'File Path Error')
self.assertIn('No help', showerror.message)
def test_path_ok_bad(self):
dialog = self.dialog
Equal = self.assertEqual
dialog.path.set(__file__ + 'bad-bad-bad')
Equal(dialog.path_ok(), None)
Equal(showerror.title, 'File Path Error')
self.assertIn('not exist', showerror.message)
def test_path_ok_web(self):
dialog = self.dialog
Equal = self.assertEqual
for url in 'www.py.org', 'http://py.org':
with self.subTest():
dialog.path.set(url)
Equal(dialog.path_ok(), url)
Equal(showerror.title, None)
def test_path_ok_file(self):
dialog = self.dialog
Equal = self.assertEqual
for platform, prefix in ('darwin', 'file://'), ('other', ''):
with self.subTest():
query.platform = platform
dialog.path.set(__file__)
Equal(dialog.path_ok(), prefix + __file__)
Equal(showerror.title, None)
class HelpsourceEntryokTest(unittest.TestCase):
"Test entry_ok method of ModuleName subclass of Query."
class Dummy_HelpSource:
entry_ok = query.HelpSource.entry_ok
def item_ok(self):
return self.name
def path_ok(self):
return self.path
dialog = Dummy_HelpSource()
def test_entry_ok_helpsource(self):
dialog = self.dialog
for name, path, result in ((None, None, None),
(None, 'doc.txt', None),
('doc', None, None),
('doc', 'doc.txt', ('doc', 'doc.txt'))):
with self.subTest():
dialog.name, dialog.path = name, path
self.assertEqual(self.dialog.entry_ok(), result)
# GUI TESTS
class QueryGuiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
requires('gui')
cls.root = root = Tk()
cls.dialog = Query(root, 'TEST', 'test', _utest=True)
cls.dialog = query.Query(root, 'TEST', 'test', _utest=True)
cls.dialog.destroy = mock.Mock()
@classmethod
@ -238,5 +372,25 @@ class ModulenameGuiTest(unittest.TestCase):
del root
class HelpsourceGuiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
requires('gui')
def test_click_help_source(self):
root = Tk()
dialog = query.HelpSource(root, 'T', menuitem='__test__',
filepath=__file__, _utest=True)
Equal = self.assertEqual
Equal(dialog.entry.get(), '__test__')
Equal(dialog.path.get(), __file__)
dialog.button_ok.invoke()
Equal(dialog.result, ('__test__', __file__))
del dialog
root.destroy()
del root
if __name__ == '__main__':
unittest.main(verbosity=2, exit=False)