blob: 001f7ee07ee256b5c7172a25fe6000e85767565a [file] [log] [blame]
Terry Jan Reedy69361592014-05-15 20:50:10 -04001'''Run human tests of Idle's window, dialog, and popup widgets.
Terry Jan Reedy06313b72014-05-11 23:32:32 -04002
Terry Jan Reedy69361592014-05-15 20:50:10 -04003run(test): run *test*, a callable that causes a widget to be displayed.
4runall(): run all tests defined in this file.
Terry Jan Reedy06313b72014-05-11 23:32:32 -04005
Terry Jan Reedy69361592014-05-15 20:50:10 -04006Let X be a global name bound to a widget callable. End the module with
7
Terry Jan Reedy06313b72014-05-11 23:32:32 -04008if __name__ == '__main__':
9 <unittest, if there is one>
10 from idlelib.idle_test.htest import run
11 run(X)
Terry Jan Reedy06313b72014-05-11 23:32:32 -040012
Terry Jan Reedy69361592014-05-15 20:50:10 -040013The X object must have a .__name__ attribute and a 'parent' parameter.
14X will often be a widget class, but a callable instance with .__name__
15or a wrapper function also work. The name of wrapper functions, like
16'_Editor_Window', should start with '_'.
17
18This file must contain a matching instance of the folling template,
19with X.__name__ prepended, as in '_Editor_window_spec ...'.
20
Terry Jan Reedy06313b72014-05-11 23:32:32 -040021_spec = {
22 'file': '',
23 'kwds': {'title': ''},
24 'msg': ""
25 }
Terry Jan Reedy69361592014-05-15 20:50:10 -040026
27file (no .py): used in runall() to import the file and get X.
28kwds: passed to X (**kwds), after 'parent' is added, to initialize X.
29title: an example; used for some widgets, delete if not.
30msg: 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 Reedy06313b72014-05-11 23:32:32 -040032'''
33from importlib import import_module
34import tkinter as tk
35
Terry Jan Reedy06313b72014-05-11 23:32:32 -040036
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
49AboutDialog_spec = {
50 'file': 'aboutDialog',
51 'kwds': {'title': 'About test'},
52 'msg': "Try each button"
53 }
54
55
56GetCfgSectionNameDialog_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 Reedy69361592014-05-15 20:50:10 -040067def run(test):
68 "Display a widget with callable *test* using a _spec dict"
Terry Jan Reedy06313b72014-05-11 23:32:32 -040069 root = tk.Tk()
Terry Jan Reedy69361592014-05-15 20:50:10 -040070 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 Reedy06313b72014-05-11 23:32:32 -040076 try:
77 print(widget.result)
78 except AttributeError:
79 pass
Terry Jan Reedy69361592014-05-15 20:50:10 -040080 tk.Label(root, text=test_spec['msg'], justify='left').pack()
81 tk.Button(root, text='Test ' + test.__name__, command=run_test).pack()
Terry Jan Reedy06313b72014-05-11 23:32:32 -040082 root.mainloop()
83
84def runall():
Terry Jan Reedy69361592014-05-15 20:50:10 -040085 "Run all tests. Quick and dirty version."
Terry Jan Reedy06313b72014-05-11 23:32:32 -040086 for k, d in globals().items():
87 if k.endswith('_spec'):
88 mod = import_module('idlelib.' + d['file'])
Terry Jan Reedy69361592014-05-15 20:50:10 -040089 test = getattr(mod, k[:-5])
90 run(test)
Terry Jan Reedy06313b72014-05-11 23:32:32 -040091
92if __name__ == '__main__':
93 runall()