htest.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. '''Run human tests of Idle's window, dialog, and popup widgets.
  2. run(*tests)
  3. Create a master Tk window. Within that, run each callable in tests
  4. after finding the matching test spec in this file. If tests is empty,
  5. run an htest for each spec dict in this file after finding the matching
  6. callable in the module named in the spec. Close the window to skip or
  7. end the test.
  8. In a tested module, let X be a global name bound to a callable (class
  9. or function) whose .__name__ attribute is also X (the usual situation).
  10. The first parameter of X must be 'parent'. When called, the parent
  11. argument will be the root window. X must create a child Toplevel
  12. window (or subclass thereof). The Toplevel may be a test widget or
  13. dialog, in which case the callable is the corresponding class. Or the
  14. Toplevel may contain the widget to be tested or set up a context in
  15. which a test widget is invoked. In this latter case, the callable is a
  16. wrapper function that sets up the Toplevel and other objects. Wrapper
  17. function names, such as _editor_window', should start with '_'.
  18. End the module with
  19. if __name__ == '__main__':
  20. <unittest, if there is one>
  21. from idlelib.idle_test.htest import run
  22. run(X)
  23. To have wrapper functions and test invocation code ignored by coveragepy
  24. reports, put '# htest #' on the def statement header line.
  25. def _wrapper(parent): # htest #
  26. Also make sure that the 'if __name__' line matches the above. Then have
  27. make sure that .coveragerc includes the following.
  28. [report]
  29. exclude_lines =
  30. .*# htest #
  31. if __name__ == .__main__.:
  32. (The "." instead of "'" is intentional and necessary.)
  33. To run any X, this file must contain a matching instance of the
  34. following template, with X.__name__ prepended to '_spec'.
  35. When all tests are run, the prefix is use to get X.
  36. _spec = {
  37. 'file': '',
  38. 'kwds': {'title': ''},
  39. 'msg': ""
  40. }
  41. file (no .py): run() imports file.py.
  42. kwds: augmented with {'parent':root} and passed to X as **kwds.
  43. title: an example kwd; some widgets need this, delete if not.
  44. msg: master window hints about testing the widget.
  45. Modules and classes not being tested at the moment:
  46. pyshell.PyShellEditorWindow
  47. debugger.Debugger
  48. autocomplete_w.AutoCompleteWindow
  49. outwin.OutputWindow (indirectly being tested with grep test)
  50. '''
  51. import idlelib.pyshell # Set Windows DPI awareness before Tk().
  52. from importlib import import_module
  53. import textwrap
  54. import tkinter as tk
  55. from tkinter.ttk import Scrollbar
  56. tk.NoDefaultRoot()
  57. AboutDialog_spec = {
  58. 'file': 'help_about',
  59. 'kwds': {'title': 'help_about test',
  60. '_htest': True,
  61. },
  62. 'msg': "Click on URL to open in default browser.\n"
  63. "Verify x.y.z versions and test each button, including Close.\n "
  64. }
  65. # TODO implement ^\; adding '<Control-Key-\\>' to function does not work.
  66. _calltip_window_spec = {
  67. 'file': 'calltip_w',
  68. 'kwds': {},
  69. 'msg': "Typing '(' should display a calltip.\n"
  70. "Typing ') should hide the calltip.\n"
  71. "So should moving cursor out of argument area.\n"
  72. "Force-open-calltip does not work here.\n"
  73. }
  74. _module_browser_spec = {
  75. 'file': 'browser',
  76. 'kwds': {},
  77. 'msg': "Inspect names of module, class(with superclass if "
  78. "applicable), methods and functions.\nToggle nested items.\n"
  79. "Double clicking on items prints a traceback for an exception "
  80. "that is ignored."
  81. }
  82. _color_delegator_spec = {
  83. 'file': 'colorizer',
  84. 'kwds': {},
  85. 'msg': "The text is sample Python code.\n"
  86. "Ensure components like comments, keywords, builtins,\n"
  87. "string, definitions, and break are correctly colored.\n"
  88. "The default color scheme is in idlelib/config-highlight.def"
  89. }
  90. CustomRun_spec = {
  91. 'file': 'query',
  92. 'kwds': {'title': 'Customize query.py Run',
  93. '_htest': True},
  94. 'msg': "Enter with <Return> or [Run]. Print valid entry to Shell\n"
  95. "Arguments are parsed into a list\n"
  96. "Mode is currently restart True or False\n"
  97. "Close dialog with valid entry, <Escape>, [Cancel], [X]"
  98. }
  99. ConfigDialog_spec = {
  100. 'file': 'configdialog',
  101. 'kwds': {'title': 'ConfigDialogTest',
  102. '_htest': True,},
  103. 'msg': "IDLE preferences dialog.\n"
  104. "In the 'Fonts/Tabs' tab, changing font face, should update the "
  105. "font face of the text in the area below it.\nIn the "
  106. "'Highlighting' tab, try different color schemes. Clicking "
  107. "items in the sample program should update the choices above it."
  108. "\nIn the 'Keys', 'General' and 'Extensions' tabs, test settings "
  109. "of interest."
  110. "\n[Ok] to close the dialog.[Apply] to apply the settings and "
  111. "and [Cancel] to revert all changes.\nRe-run the test to ensure "
  112. "changes made have persisted."
  113. }
  114. # TODO Improve message
  115. _dyn_option_menu_spec = {
  116. 'file': 'dynoption',
  117. 'kwds': {},
  118. 'msg': "Select one of the many options in the 'old option set'.\n"
  119. "Click the button to change the option set.\n"
  120. "Select one of the many options in the 'new option set'."
  121. }
  122. # TODO edit wrapper
  123. _editor_window_spec = {
  124. 'file': 'editor',
  125. 'kwds': {},
  126. 'msg': "Test editor functions of interest.\n"
  127. "Best to close editor first."
  128. }
  129. GetKeysDialog_spec = {
  130. 'file': 'config_key',
  131. 'kwds': {'title': 'Test keybindings',
  132. 'action': 'find-again',
  133. 'current_key_sequences': [['<Control-Key-g>', '<Key-F3>', '<Control-Key-G>']],
  134. '_htest': True,
  135. },
  136. 'msg': "Test for different key modifier sequences.\n"
  137. "<nothing> is invalid.\n"
  138. "No modifier key is invalid.\n"
  139. "Shift key with [a-z],[0-9], function key, move key, tab, space "
  140. "is invalid.\nNo validity checking if advanced key binding "
  141. "entry is used."
  142. }
  143. _grep_dialog_spec = {
  144. 'file': 'grep',
  145. 'kwds': {},
  146. 'msg': "Click the 'Show GrepDialog' button.\n"
  147. "Test the various 'Find-in-files' functions.\n"
  148. "The results should be displayed in a new '*Output*' window.\n"
  149. "'Right-click'->'Go to file/line' anywhere in the search results "
  150. "should open that file \nin a new EditorWindow."
  151. }
  152. HelpSource_spec = {
  153. 'file': 'query',
  154. 'kwds': {'title': 'Help name and source',
  155. 'menuitem': 'test',
  156. 'filepath': __file__,
  157. 'used_names': {'abc'},
  158. '_htest': True},
  159. 'msg': "Enter menu item name and help file path\n"
  160. "'', > than 30 chars, and 'abc' are invalid menu item names.\n"
  161. "'' and file does not exist are invalid path items.\n"
  162. "Any url ('www...', 'http...') is accepted.\n"
  163. "Test Browse with and without path, as cannot unittest.\n"
  164. "[Ok] or <Return> prints valid entry to shell\n"
  165. "[Cancel] or <Escape> prints None to shell"
  166. }
  167. _io_binding_spec = {
  168. 'file': 'iomenu',
  169. 'kwds': {},
  170. 'msg': "Test the following bindings.\n"
  171. "<Control-o> to open file from dialog.\n"
  172. "Edit the file.\n"
  173. "<Control-p> to print the file.\n"
  174. "<Control-s> to save the file.\n"
  175. "<Alt-s> to save-as another file.\n"
  176. "<Control-c> to save-copy-as another file.\n"
  177. "Check that changes were saved by opening the file elsewhere."
  178. }
  179. _linenumbers_drag_scrolling_spec = {
  180. 'file': 'sidebar',
  181. 'kwds': {},
  182. 'msg': textwrap.dedent("""\
  183. 1. Click on the line numbers and drag down below the edge of the
  184. window, moving the mouse a bit and then leaving it there for a while.
  185. The text and line numbers should gradually scroll down, with the
  186. selection updated continuously.
  187. 2. With the lines still selected, click on a line number above the
  188. selected lines. Only the line whose number was clicked should be
  189. selected.
  190. 3. Repeat step #1, dragging to above the window. The text and line
  191. numbers should gradually scroll up, with the selection updated
  192. continuously.
  193. 4. Repeat step #2, clicking a line number below the selection."""),
  194. }
  195. _multi_call_spec = {
  196. 'file': 'multicall',
  197. 'kwds': {},
  198. 'msg': "The following actions should trigger a print to console or IDLE"
  199. " Shell.\nEntering and leaving the text area, key entry, "
  200. "<Control-Key>,\n<Alt-Key-a>, <Control-Key-a>, "
  201. "<Alt-Control-Key-a>, \n<Control-Button-1>, <Alt-Button-1> and "
  202. "focusing out of the window\nare sequences to be tested."
  203. }
  204. _multistatus_bar_spec = {
  205. 'file': 'statusbar',
  206. 'kwds': {},
  207. 'msg': "Ensure presence of multi-status bar below text area.\n"
  208. "Click 'Update Status' to change the multi-status text"
  209. }
  210. _object_browser_spec = {
  211. 'file': 'debugobj',
  212. 'kwds': {},
  213. 'msg': "Double click on items up to the lowest level.\n"
  214. "Attributes of the objects and related information "
  215. "will be displayed side-by-side at each level."
  216. }
  217. _path_browser_spec = {
  218. 'file': 'pathbrowser',
  219. 'kwds': {},
  220. 'msg': "Test for correct display of all paths in sys.path.\n"
  221. "Toggle nested items up to the lowest level.\n"
  222. "Double clicking on an item prints a traceback\n"
  223. "for an exception that is ignored."
  224. }
  225. _percolator_spec = {
  226. 'file': 'percolator',
  227. 'kwds': {},
  228. 'msg': "There are two tracers which can be toggled using a checkbox.\n"
  229. "Toggling a tracer 'on' by checking it should print tracer "
  230. "output to the console or to the IDLE shell.\n"
  231. "If both the tracers are 'on', the output from the tracer which "
  232. "was switched 'on' later, should be printed first\n"
  233. "Test for actions like text entry, and removal."
  234. }
  235. Query_spec = {
  236. 'file': 'query',
  237. 'kwds': {'title': 'Query',
  238. 'message': 'Enter something',
  239. 'text0': 'Go',
  240. '_htest': True},
  241. 'msg': "Enter with <Return> or [Ok]. Print valid entry to Shell\n"
  242. "Blank line, after stripping, is ignored\n"
  243. "Close dialog with valid entry, <Escape>, [Cancel], [X]"
  244. }
  245. _replace_dialog_spec = {
  246. 'file': 'replace',
  247. 'kwds': {},
  248. 'msg': "Click the 'Replace' button.\n"
  249. "Test various replace options in the 'Replace dialog'.\n"
  250. "Click [Close] or [X] to close the 'Replace Dialog'."
  251. }
  252. _search_dialog_spec = {
  253. 'file': 'search',
  254. 'kwds': {},
  255. 'msg': "Click the 'Search' button.\n"
  256. "Test various search options in the 'Search dialog'.\n"
  257. "Click [Close] or [X] to close the 'Search Dialog'."
  258. }
  259. _searchbase_spec = {
  260. 'file': 'searchbase',
  261. 'kwds': {},
  262. 'msg': "Check the appearance of the base search dialog\n"
  263. "Its only action is to close."
  264. }
  265. _scrolled_list_spec = {
  266. 'file': 'scrolledlist',
  267. 'kwds': {},
  268. 'msg': "You should see a scrollable list of items\n"
  269. "Selecting (clicking) or double clicking an item "
  270. "prints the name to the console or Idle shell.\n"
  271. "Right clicking an item will display a popup."
  272. }
  273. show_idlehelp_spec = {
  274. 'file': 'help',
  275. 'kwds': {},
  276. 'msg': "If the help text displays, this works.\n"
  277. "Text is selectable. Window is scrollable."
  278. }
  279. _stack_viewer_spec = {
  280. 'file': 'stackviewer',
  281. 'kwds': {},
  282. 'msg': "A stacktrace for a NameError exception.\n"
  283. "Expand 'idlelib ...' and '<locals>'.\n"
  284. "Check that exc_value, exc_tb, and exc_type are correct.\n"
  285. }
  286. _tooltip_spec = {
  287. 'file': 'tooltip',
  288. 'kwds': {},
  289. 'msg': "Place mouse cursor over both the buttons\n"
  290. "A tooltip should appear with some text."
  291. }
  292. _tree_widget_spec = {
  293. 'file': 'tree',
  294. 'kwds': {},
  295. 'msg': "The canvas is scrollable.\n"
  296. "Click on folders up to to the lowest level."
  297. }
  298. _undo_delegator_spec = {
  299. 'file': 'undo',
  300. 'kwds': {},
  301. 'msg': "Click [Undo] to undo any action.\n"
  302. "Click [Redo] to redo any action.\n"
  303. "Click [Dump] to dump the current state "
  304. "by printing to the console or the IDLE shell.\n"
  305. }
  306. ViewWindow_spec = {
  307. 'file': 'textview',
  308. 'kwds': {'title': 'Test textview',
  309. 'contents': 'The quick brown fox jumps over the lazy dog.\n'*35,
  310. '_htest': True},
  311. 'msg': "Test for read-only property of text.\n"
  312. "Select text, scroll window, close"
  313. }
  314. _widget_redirector_spec = {
  315. 'file': 'redirector',
  316. 'kwds': {},
  317. 'msg': "Every text insert should be printed to the console "
  318. "or the IDLE shell."
  319. }
  320. def run(*tests):
  321. root = tk.Tk()
  322. root.title('IDLE htest')
  323. root.resizable(0, 0)
  324. # a scrollable Label like constant width text widget.
  325. frameLabel = tk.Frame(root, padx=10)
  326. frameLabel.pack()
  327. text = tk.Text(frameLabel, wrap='word')
  328. text.configure(bg=root.cget('bg'), relief='flat', height=4, width=70)
  329. scrollbar = Scrollbar(frameLabel, command=text.yview)
  330. text.config(yscrollcommand=scrollbar.set)
  331. scrollbar.pack(side='right', fill='y', expand=False)
  332. text.pack(side='left', fill='both', expand=True)
  333. test_list = [] # List of tuples of the form (spec, callable widget)
  334. if tests:
  335. for test in tests:
  336. test_spec = globals()[test.__name__ + '_spec']
  337. test_spec['name'] = test.__name__
  338. test_list.append((test_spec, test))
  339. else:
  340. for k, d in globals().items():
  341. if k.endswith('_spec'):
  342. test_name = k[:-5]
  343. test_spec = d
  344. test_spec['name'] = test_name
  345. mod = import_module('idlelib.' + test_spec['file'])
  346. test = getattr(mod, test_name)
  347. test_list.append((test_spec, test))
  348. test_name = tk.StringVar(root)
  349. callable_object = None
  350. test_kwds = None
  351. def next_test():
  352. nonlocal test_name, callable_object, test_kwds
  353. if len(test_list) == 1:
  354. next_button.pack_forget()
  355. test_spec, callable_object = test_list.pop()
  356. test_kwds = test_spec['kwds']
  357. test_kwds['parent'] = root
  358. test_name.set('Test ' + test_spec['name'])
  359. text.configure(state='normal') # enable text editing
  360. text.delete('1.0','end')
  361. text.insert("1.0",test_spec['msg'])
  362. text.configure(state='disabled') # preserve read-only property
  363. def run_test(_=None):
  364. widget = callable_object(**test_kwds)
  365. try:
  366. print(widget.result)
  367. except AttributeError:
  368. pass
  369. def close(_=None):
  370. root.destroy()
  371. button = tk.Button(root, textvariable=test_name,
  372. default='active', command=run_test)
  373. next_button = tk.Button(root, text="Next", command=next_test)
  374. button.pack()
  375. next_button.pack()
  376. next_button.focus_set()
  377. root.bind('<Key-Return>', run_test)
  378. root.bind('<Key-Escape>', close)
  379. next_test()
  380. root.mainloop()
  381. if __name__ == '__main__':
  382. run()