|
1 | | -'''Run a human test of Idle wndow, dialog, and other widget classes. |
| 1 | +'''Run human tests of Idle's window, dialog, and popup widgets. |
2 | 2 |
|
3 | | -run(klass) runs a test for one class. |
4 | | -runall() runs all the defined tests |
| 3 | +run(test): run *test*, a callable that causes a widget to be displayed. |
| 4 | +runall(): run all tests defined in this file. |
| 5 | +
|
| 6 | +Let X be a global name bound to a widget callable. End the module with |
5 | 7 |
|
6 | | -The file wih the widget class should end with |
7 | 8 | if __name__ == '__main__': |
8 | 9 | <unittest, if there is one> |
9 | 10 | from idlelib.idle_test.htest import run |
10 | 11 | run(X) |
11 | | -where X is a global object of the module. X must be a callable with a |
12 | | -.__name__ attribute that accepts a 'parent' attribute. X will usually be |
13 | | -a widget class, but a callable instance with .__name__ or a wrapper |
14 | | -function also work. The name of wrapper functions, like _Editor_Window, |
15 | | -should start with '_'. |
16 | 12 |
|
17 | | -This file must then contain an instance of this template. |
| 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 | +
|
18 | 21 | _spec = { |
19 | 22 | 'file': '', |
20 | 23 | 'kwds': {'title': ''}, |
21 | 24 | 'msg': "" |
22 | 25 | } |
23 | | -with X.__name__ prepended to _spec. |
24 | | -File (no .py) is used in runall() to import the file and get the class. |
25 | | -Kwds is passed to X (**kwds) after 'parent' is added, to initialize X. |
26 | | -Msg. displayed is a window with a start button. hint as to how the user |
27 | | -might test the widget. Closing The box skips or ends the test. |
| 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. |
28 | 32 | ''' |
29 | 33 | from importlib import import_module |
30 | 34 | import tkinter as tk |
31 | 35 |
|
32 | | -# Template for class_spec dicts, copy and uncomment |
33 | 36 |
|
34 | 37 | _Editor_window_spec = { |
35 | 38 | 'file': 'EditorWindow', |
|
61 | 64 | "Close 'Get Name' with a valid entry (printed to Shell), [Cancel], or [X]", |
62 | 65 | } |
63 | 66 |
|
64 | | -def run(klas): |
65 | | - "Test the widget class klas using _spec dict" |
| 67 | +def run(test): |
| 68 | + "Display a widget with callable *test* using a _spec dict" |
66 | 69 | root = tk.Tk() |
67 | | - klas_spec = globals()[klas.__name__+'_spec'] |
68 | | - klas_kwds = klas_spec['kwds'] |
69 | | - klas_kwds['parent'] = root |
70 | | - # This presumes that Idle consistently uses 'parent' |
71 | | - def run_klas(): |
72 | | - widget = klas(**klas_kwds) |
| 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) |
73 | 76 | try: |
74 | 77 | print(widget.result) |
75 | 78 | except AttributeError: |
76 | 79 | pass |
77 | | - tk.Label(root, text=klas_spec['msg'], justify='left').pack() |
78 | | - tk.Button(root, text='Test ' + klas.__name__, command=run_klas).pack() |
| 80 | + tk.Label(root, text=test_spec['msg'], justify='left').pack() |
| 81 | + tk.Button(root, text='Test ' + test.__name__, command=run_test).pack() |
79 | 82 | root.mainloop() |
80 | 83 |
|
81 | 84 | def runall(): |
82 | | - 'Run all tests. Quick and dirty version.' |
| 85 | + "Run all tests. Quick and dirty version." |
83 | 86 | for k, d in globals().items(): |
84 | 87 | if k.endswith('_spec'): |
85 | 88 | mod = import_module('idlelib.' + d['file']) |
86 | | - klas = getattr(mod, k[:-5]) |
87 | | - run(klas) |
| 89 | + test = getattr(mod, k[:-5]) |
| 90 | + run(test) |
88 | 91 |
|
89 | 92 | if __name__ == '__main__': |
90 | 93 | runall() |
0 commit comments