Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 1 | '''Run human tests of Idle's window, dialog, and popup widgets. |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 2 | |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 3 | run(test): run *test*, a callable that causes a widget to be displayed. |
| 4 | runall(): run all tests defined in this file. |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 5 | |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 6 | Let X be a global name bound to a widget callable. End the module with |
| 7 | |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 8 | if __name__ == '__main__': |
| 9 | <unittest, if there is one> |
| 10 | from idlelib.idle_test.htest import run |
| 11 | run(X) |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 12 | |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 13 | The X object must have a .__name__ attribute and a 'parent' parameter. |
| 14 | X will often be a widget class, but a callable instance with .__name__ |
| 15 | or a wrapper function also work. The name of wrapper functions, like |
| 16 | '_Editor_Window', should start with '_'. |
| 17 | |
| 18 | This file must contain a matching instance of the folling template, |
| 19 | with X.__name__ prepended, as in '_Editor_window_spec ...'. |
| 20 | |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 21 | _spec = { |
| 22 | 'file': '', |
| 23 | 'kwds': {'title': ''}, |
| 24 | 'msg': "" |
| 25 | } |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 26 | |
| 27 | file (no .py): used in runall() to import the file and get X. |
| 28 | kwds: passed to X (**kwds), after 'parent' is added, to initialize X. |
| 29 | title: an example; used for some widgets, delete if not. |
| 30 | msg: displayed in a master window. Hints as to how the user might |
| 31 | test the widget. Close the window to skip or end the test. |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 32 | ''' |
| 33 | from importlib import import_module |
| 34 | import tkinter as tk |
| 35 | |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 36 | |
| 37 | _Editor_window_spec = { |
| 38 | 'file': 'EditorWindow', |
| 39 | 'kwds': {}, |
| 40 | 'msg': "Test editor functions of interest" |
| 41 | } |
| 42 | |
| 43 | _Help_dialog_spec = { |
| 44 | 'file': 'EditorWindow', |
| 45 | 'kwds': {}, |
| 46 | 'msg': "If the help text displays, this works" |
| 47 | } |
| 48 | |
| 49 | AboutDialog_spec = { |
| 50 | 'file': 'aboutDialog', |
| 51 | 'kwds': {'title': 'About test'}, |
| 52 | 'msg': "Try each button" |
| 53 | } |
| 54 | |
| 55 | |
| 56 | GetCfgSectionNameDialog_spec = { |
| 57 | 'file': 'configSectionNameDialog', |
| 58 | 'kwds': {'title':'Get Name', |
| 59 | 'message':'Enter something', |
| 60 | 'used_names': {'abc'}, |
| 61 | '_htest': True}, |
| 62 | 'msg': "After the text entered with [Ok] is stripped, <nothing>, " |
| 63 | "'abc', or more that 30 chars are errors.\n" |
| 64 | "Close 'Get Name' with a valid entry (printed to Shell), [Cancel], or [X]", |
| 65 | } |
| 66 | |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 67 | def run(test): |
| 68 | "Display a widget with callable *test* using a _spec dict" |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 69 | root = tk.Tk() |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 70 | test_spec = globals()[test.__name__ + '_spec'] |
| 71 | test_kwds = test_spec['kwds'] |
| 72 | test_kwds['parent'] = root |
| 73 | |
| 74 | def run_test(): |
| 75 | widget = test(**test_kwds) |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 76 | try: |
| 77 | print(widget.result) |
| 78 | except AttributeError: |
| 79 | pass |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 80 | tk.Label(root, text=test_spec['msg'], justify='left').pack() |
| 81 | tk.Button(root, text='Test ' + test.__name__, command=run_test).pack() |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 82 | root.mainloop() |
| 83 | |
| 84 | def runall(): |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 85 | "Run all tests. Quick and dirty version." |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 86 | for k, d in globals().items(): |
| 87 | if k.endswith('_spec'): |
| 88 | mod = import_module('idlelib.' + d['file']) |
Terry Jan Reedy | 6936159 | 2014-05-15 20:50:10 -0400 | [diff] [blame^] | 89 | test = getattr(mod, k[:-5]) |
| 90 | run(test) |
Terry Jan Reedy | 06313b7 | 2014-05-11 23:32:32 -0400 | [diff] [blame] | 91 | |
| 92 | if __name__ == '__main__': |
| 93 | runall() |