From 00b0bd55b4d615740e06ac4d445221a176da30f3 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 11 May 2014 23:32:20 -0400 Subject: [PATCH] Issue #18104: Add idlelib/idle_test/htest.py with a few sample tests to begin consolidating and improving human-validated tests of Idle. Change other files as needed to work with htest. Running the module as __main__ runs all tests. --- Lib/idlelib/EditorWindow.py | 14 ++-- Lib/idlelib/aboutDialog.py | 11 +--- Lib/idlelib/configSectionNameDialog.py | 28 +++----- Lib/idlelib/idle_test/htest.py | 91 ++++++++++++++++++++++++++ Misc/NEWS | 4 ++ 5 files changed, 117 insertions(+), 31 deletions(-) create mode 100644 Lib/idlelib/idle_test/htest.py diff --git a/Lib/idlelib/EditorWindow.py b/Lib/idlelib/EditorWindow.py index 21efc31606c..634a2266c40 100644 --- a/Lib/idlelib/EditorWindow.py +++ b/Lib/idlelib/EditorWindow.py @@ -108,6 +108,8 @@ class HelpDialog(object): self.parent = None helpDialog = HelpDialog() # singleton instance +def _Help_dialog(parent): # wrapper for htest + helpDialog.show_dialog(parent) class EditorWindow(object): @@ -1709,19 +1711,21 @@ def fixwordbreaks(root): tk.call('set', 'tcl_nonwordchars', '[^a-zA-Z0-9_]') -def test(): - root = Tk() +def _Editor_window(parent): + root = parent fixwordbreaks(root) root.withdraw() if sys.argv[1:]: filename = sys.argv[1] else: filename = None + macosxSupport.setupApp(root, None) edit = EditorWindow(root=root, filename=filename) edit.set_close_hook(root.quit) edit.text.bind("<>", edit.close_event) - root.mainloop() - root.destroy() if __name__ == '__main__': - test() + from idlelib.idle_test.htest import run + if len(sys.argv) <= 1: + run(_Help_dialog) + run(_Editor_window) diff --git a/Lib/idlelib/aboutDialog.py b/Lib/idlelib/aboutDialog.py index 07345180f91..05631fa264d 100644 --- a/Lib/idlelib/aboutDialog.py +++ b/Lib/idlelib/aboutDialog.py @@ -12,7 +12,7 @@ class AboutDialog(Toplevel): """Modal about dialog for idle """ - def __init__(self,parent,title): + def __init__(self, parent, title): Toplevel.__init__(self, parent) self.configure(borderwidth=5) self.geometry("+%d+%d" % (parent.winfo_rootx()+30, @@ -136,10 +136,5 @@ class AboutDialog(Toplevel): self.destroy() if __name__ == '__main__': - # test the dialog - root = Tk() - def run(): - from idlelib import aboutDialog - aboutDialog.AboutDialog(root, 'About') - Button(root, text='Dialog', command=run).pack() - root.mainloop() + from idlelib.idle_test.htest import run + run(AboutDialog) diff --git a/Lib/idlelib/configSectionNameDialog.py b/Lib/idlelib/configSectionNameDialog.py index 04fcd8cf9a9..c09dca896b0 100644 --- a/Lib/idlelib/configSectionNameDialog.py +++ b/Lib/idlelib/configSectionNameDialog.py @@ -7,10 +7,11 @@ is the .result attribute set in the Ok and Cancel methods. from Tkinter import * import tkMessageBox class GetCfgSectionNameDialog(Toplevel): - def __init__(self, parent, title, message, used_names): + def __init__(self, parent, title, message, used_names, _htest=False): """ message - string, informational message to display used_names - string collection, names already in use for validity check + _htest - bool, change box location when running htest """ Toplevel.__init__(self, parent) self.configure(borderwidth=5) @@ -29,11 +30,12 @@ class GetCfgSectionNameDialog(Toplevel): self.messageInfo.config(width=self.frameMain.winfo_reqwidth()) self.geometry( "+%d+%d" % ( - parent.winfo_rootx() + - (parent.winfo_width()/2 - self.winfo_reqwidth()/2), - parent.winfo_rooty() + - (parent.winfo_height()/2 - self.winfo_reqheight()/2) - ) ) #centre dialog over parent + parent.winfo_rootx() + + (parent.winfo_width()/2 - self.winfo_reqwidth()/2), + parent.winfo_rooty() + + ((parent.winfo_height()/2 - self.winfo_reqheight()/2) + if not _htest else 100) + ) ) #centre dialog over parent (or below htest box) self.deiconify() #geometry set, unhide self.wait_window() def create_widgets(self): @@ -86,15 +88,5 @@ if __name__ == '__main__': import unittest unittest.main('idlelib.idle_test.test_config_name', verbosity=2, exit=False) - # also human test the dialog - root = Tk() - def run(): - dlg=GetCfgSectionNameDialog(root,'Get Name', - "After the text entered with [Ok] is stripped, , " - "'abc', or more that 30 chars are errors. " - "Close with a valid entry (printed), [Cancel], or [X]", - {'abc'}) - print dlg.result - Message(root, text='').pack() # will be needed for oher dialog tests - Button(root, text='Click to begin dialog test', command=run).pack() - root.mainloop() + from idlelib.idle_test.htest import run + run(GetCfgSectionNameDialog) diff --git a/Lib/idlelib/idle_test/htest.py b/Lib/idlelib/idle_test/htest.py new file mode 100644 index 00000000000..ac1be5c616f --- /dev/null +++ b/Lib/idlelib/idle_test/htest.py @@ -0,0 +1,91 @@ +'''Run a human test of Idle wndow, dialog, and other widget classes. + +run(klass) runs a test for one class. +runall() runs all the defined tests + +The file wih the widget class should end with +if __name__ == '__main__': + + from idlelib.idle_test.htest import run + run(X) +where X is a global object of the module. X must be a callable with a +.__name__ attribute that accepts a 'parent' attribute. X will usually be +a widget class, but a callable instance with .__name__ or a wrapper +function also work. The name of wrapper functions, like _Editor_Window, +should start with '_'. + +This file must then contain an instance of this template. +_spec = { + 'file': '', + 'kwds': {'title': ''}, + 'msg': "" + } +with X.__name__ prepended to _spec. +File (no .py) is used in runall() to import the file and get the class. +Kwds is passed to X (**kwds) after 'parent' is added, to initialize X. +Msg. displayed is a window with a start button. hint as to how the user +might test the widget. Closing The box skips or ends the test. +''' +from importlib import import_module +import Tkinter as tk + +# Template for class_spec dicts, copy and uncomment + +_Editor_window_spec = { + 'file': 'EditorWindow', + 'kwds': {}, + 'msg': "Test editor functions of interest" + } + +_Help_dialog_spec = { + 'file': 'EditorWindow', + 'kwds': {}, + 'msg': "If the help text displays, this works" + } + +AboutDialog_spec = { + 'file': 'aboutDialog', + 'kwds': {'title': 'About test'}, + 'msg': "Try each button" + } + + +GetCfgSectionNameDialog_spec = { + 'file': 'configSectionNameDialog', + 'kwds': {'title':'Get Name', + 'message':'Enter something', + 'used_names': {'abc'}, + '_htest': True}, + 'msg': "After the text entered with [Ok] is stripped, , " + "'abc', or more that 30 chars are errors.\n" + "Close 'Get Name' with a valid entry (printed to Shell), [Cancel], or [X]", + } + +def run(klas): + "Test the widget class klas using _spec dict" + root = tk.Tk() + klas_spec = globals()[klas.__name__+'_spec'] + klas_kwds = klas_spec['kwds'] + klas_kwds['parent'] = root + # This presumes that Idle consistently uses 'parent' + def run_klas(): + widget = klas(**klas_kwds) + try: + print(widget.result) + except AttributeError: + pass + tk.Label(root, text=klas_spec['msg'], justify='left').pack() + tk.Button(root, text='Test ' + klas.__name__, command=run_klas).pack() + root.mainloop() + +def runall(): + 'Run all tests. Quick and dirty version.' + for k, d in globals().items(): + if k.endswith('_spec'): + mod = import_module('idlelib.' + d['file']) + klas = getattr(mod, k[:-5]) + run(klas) + +if __name__ == '__main__': + runall() + diff --git a/Misc/NEWS b/Misc/NEWS index 65489562da5..5d3209d955e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -308,6 +308,10 @@ Tools/Demos IDLE ---- +- Issue #18104: Add idlelib/idle_test/htest.py with a few sample tests to begin + consolidating and improving human-validated tests of Idle. Change other files + as needed to work with htest. Running the module as __main__ runs all tests. + - Issue #21139: Change default paragraph width to 72, the PEP 8 recommendation. - Issue #21284: Paragraph reformat test passes after user changes reformat width.