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