Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- llava/lib/python3.10/idlelib/Icons/folder.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_16.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_16.png +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_256.png +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_32.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_32.png +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_48.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/idle_48.png +3 -0
- llava/lib/python3.10/idlelib/Icons/minusnode.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/openfolder.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/plusnode.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/python.gif +3 -0
- llava/lib/python3.10/idlelib/Icons/tk.gif +3 -0
- llava/lib/python3.10/idlelib/idle_test/README.txt +238 -0
- llava/lib/python3.10/idlelib/idle_test/__init__.py +17 -0
- llava/lib/python3.10/idlelib/idle_test/example_noext +4 -0
- llava/lib/python3.10/idlelib/idle_test/mock_tk.py +307 -0
- llava/lib/python3.10/idlelib/idle_test/test_browser.py +257 -0
- llava/lib/python3.10/idlelib/idle_test/test_colorizer.py +622 -0
- llava/lib/python3.10/idlelib/idle_test/test_debugger_r.py +36 -0
- llava/lib/python3.10/idlelib/idle_test/test_editor.py +211 -0
- llava/lib/python3.10/idlelib/idle_test/test_filelist.py +33 -0
- llava/lib/python3.10/idlelib/idle_test/test_format.py +668 -0
- llava/lib/python3.10/idlelib/idle_test/test_hyperparser.py +276 -0
- llava/lib/python3.10/idlelib/idle_test/test_iomenu.py +78 -0
- llava/lib/python3.10/idlelib/idle_test/test_pyshell.py +148 -0
- llava/lib/python3.10/idlelib/idle_test/test_query.py +451 -0
- llava/lib/python3.10/idlelib/idle_test/test_redirector.py +122 -0
- llava/lib/python3.10/idlelib/idle_test/test_replace.py +294 -0
- llava/lib/python3.10/idlelib/idle_test/test_run.py +429 -0
- llava/lib/python3.10/idlelib/idle_test/test_searchbase.py +160 -0
- llava/lib/python3.10/idlelib/idle_test/test_sidebar.py +768 -0
- llava/lib/python3.10/idlelib/idle_test/test_squeezer.py +467 -0
- llava/lib/python3.10/idlelib/idle_test/test_stackviewer.py +47 -0
- llava/lib/python3.10/idlelib/idle_test/test_text.py +236 -0
- llava/lib/python3.10/idlelib/idle_test/test_textview.py +233 -0
- llava/lib/python3.10/idlelib/idle_test/test_tooltip.py +161 -0
- llava/lib/python3.10/idlelib/idle_test/test_tree.py +60 -0
- llava/lib/python3.10/idlelib/idle_test/test_util.py +14 -0
- llava/lib/python3.10/idlelib/idle_test/test_warning.py +73 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_backward_compositeimplicitautograd_dispatch.h +23 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_fft_c2c_cuda_dispatch.h +28 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_asin_cuda_dispatch.h +24 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_lgamma.h +44 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_pow_compositeexplicitautograd_dispatch.h +35 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_remove_batch_dim_compositeimplicitautograd_dispatch.h +23 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_sparse_addmm_native.h +22 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_sparse_softmax_ops.h +61 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_upsample_nearest_exact1d_backward_native.h +26 -0
- parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_values_copy.h +39 -0
llava/lib/python3.10/idlelib/Icons/folder.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_16.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_16.png
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_256.png
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_32.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_32.png
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_48.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/idle_48.png
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/minusnode.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/openfolder.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/plusnode.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/python.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/Icons/tk.gif
ADDED
|
|
Git LFS Details
|
llava/lib/python3.10/idlelib/idle_test/README.txt
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 4 |
+
|
| 5 |
+
Automated unit tests were added in 3.3 for Python 3.x.
|
| 6 |
+
To run the tests from a command line:
|
| 7 |
+
|
| 8 |
+
python -m test.test_idle
|
| 9 |
+
|
| 10 |
+
Human-mediated tests were added later in 3.4.
|
| 11 |
+
|
| 12 |
+
python -m idlelib.idle_test.htest
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
1. Test Files
|
| 16 |
+
|
| 17 |
+
The idle directory, idlelib, has over 60 xyz.py files. The idle_test
|
| 18 |
+
subdirectory contains test_xyz.py for each implementation file xyz.py.
|
| 19 |
+
To add a test for abc.py, open idle_test/template.py and immediately
|
| 20 |
+
Save As test_abc.py. Insert 'abc' on the first line, and replace
|
| 21 |
+
'zzdummy' with 'abc.
|
| 22 |
+
|
| 23 |
+
Remove the imports of requires and tkinter if not needed. Otherwise,
|
| 24 |
+
add to the tkinter imports as needed.
|
| 25 |
+
|
| 26 |
+
Add a prefix to 'Test' for the initial test class. The template class
|
| 27 |
+
contains code needed or possibly needed for gui tests. See the next
|
| 28 |
+
section if doing gui tests. If not, and not needed for further classes,
|
| 29 |
+
this code can be removed.
|
| 30 |
+
|
| 31 |
+
Add the following at the end of abc.py. If an htest was added first,
|
| 32 |
+
insert the import and main lines before the htest lines.
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
from unittest import main
|
| 36 |
+
main('idlelib.idle_test.test_abc', verbosity=2, exit=False)
|
| 37 |
+
|
| 38 |
+
The ', exit=False' is only needed if an htest follows.
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
2. GUI Tests
|
| 43 |
+
|
| 44 |
+
When run as part of the Python test suite, Idle GUI tests need to run
|
| 45 |
+
test.support.requires('gui'). A test is a GUI test if it creates a
|
| 46 |
+
tkinter.Tk root or master object either directly or indirectly by
|
| 47 |
+
instantiating a tkinter or idle class. GUI tests cannot run in test
|
| 48 |
+
processes that either have no graphical environment available or are not
|
| 49 |
+
allowed to use it.
|
| 50 |
+
|
| 51 |
+
To guard a module consisting entirely of GUI tests, start with
|
| 52 |
+
|
| 53 |
+
from test.support import requires
|
| 54 |
+
requires('gui')
|
| 55 |
+
|
| 56 |
+
To guard a test class, put "requires('gui')" in its setUpClass function.
|
| 57 |
+
The template.py file does this.
|
| 58 |
+
|
| 59 |
+
To avoid interfering with other GUI tests, all GUI objects must be
|
| 60 |
+
destroyed and deleted by the end of the test. The Tk root created in a
|
| 61 |
+
setUpX function should be destroyed in the corresponding tearDownX and
|
| 62 |
+
the module or class attribute deleted. Others widgets should descend
|
| 63 |
+
from the single root and the attributes deleted BEFORE root is
|
| 64 |
+
destroyed. See https://bugs.python.org/issue20567.
|
| 65 |
+
|
| 66 |
+
@classmethod
|
| 67 |
+
def setUpClass(cls):
|
| 68 |
+
requires('gui')
|
| 69 |
+
cls.root = tk.Tk()
|
| 70 |
+
cls.text = tk.Text(root)
|
| 71 |
+
|
| 72 |
+
@classmethod
|
| 73 |
+
def tearDownClass(cls):
|
| 74 |
+
del cls.text
|
| 75 |
+
cls.root.update_idletasks()
|
| 76 |
+
cls.root.destroy()
|
| 77 |
+
del cls.root
|
| 78 |
+
|
| 79 |
+
The update_idletasks call is sometimes needed to prevent the following
|
| 80 |
+
warning either when running a test alone or as part of the test suite
|
| 81 |
+
(#27196). It should not hurt if not needed.
|
| 82 |
+
|
| 83 |
+
can't invoke "event" command: application has been destroyed
|
| 84 |
+
...
|
| 85 |
+
"ttk::ThemeChanged"
|
| 86 |
+
|
| 87 |
+
If a test creates instance 'e' of EditorWindow, call 'e._close()' before
|
| 88 |
+
or as the first part of teardown. The effect of omitting this depends
|
| 89 |
+
on the later shutdown. Then enable the after_cancel loop in the
|
| 90 |
+
template. This prevents messages like the following.
|
| 91 |
+
|
| 92 |
+
bgerror failed to handle background error.
|
| 93 |
+
Original error: invalid command name "106096696timer_event"
|
| 94 |
+
Error in bgerror: can't invoke "tk" command: application has been destroyed
|
| 95 |
+
|
| 96 |
+
Requires('gui') causes the test(s) it guards to be skipped if any of
|
| 97 |
+
these conditions are met:
|
| 98 |
+
|
| 99 |
+
- The tests are being run by regrtest.py, and it was started without
|
| 100 |
+
enabling the "gui" resource with the "-u" command line option.
|
| 101 |
+
|
| 102 |
+
- The tests are being run on Windows by a service that is not allowed
|
| 103 |
+
to interact with the graphical environment.
|
| 104 |
+
|
| 105 |
+
- The tests are being run on Linux and X Windows is not available.
|
| 106 |
+
|
| 107 |
+
- The tests are being run on Mac OSX in a process that cannot make a
|
| 108 |
+
window manager connection.
|
| 109 |
+
|
| 110 |
+
- tkinter.Tk cannot be successfully instantiated for some reason.
|
| 111 |
+
|
| 112 |
+
- test.support.use_resources has been set by something other than
|
| 113 |
+
regrtest.py and does not contain "gui".
|
| 114 |
+
|
| 115 |
+
Tests of non-GUI operations should avoid creating tk widgets. Incidental
|
| 116 |
+
uses of tk variables and messageboxes can be replaced by the mock
|
| 117 |
+
classes in idle_test/mock_tk.py. The mock text handles some uses of the
|
| 118 |
+
tk Text widget.
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
3. Running Unit Tests
|
| 122 |
+
|
| 123 |
+
Assume that xyz.py and test_xyz.py both end with a unittest.main() call.
|
| 124 |
+
Running either from an Idle editor runs all tests in the test_xyz file
|
| 125 |
+
with the version of Python running Idle. Test output appears in the
|
| 126 |
+
Shell window. The 'verbosity=2' option lists all test methods in the
|
| 127 |
+
file, which is appropriate when developing tests. The 'exit=False'
|
| 128 |
+
option is needed in xyx.py files when an htest follows.
|
| 129 |
+
|
| 130 |
+
The following command lines also run all test methods, including
|
| 131 |
+
GUI tests, in test_xyz.py. (Both '-m idlelib' and '-m idlelib.idle'
|
| 132 |
+
start Idle and so cannot run tests.)
|
| 133 |
+
|
| 134 |
+
python -m idlelib.xyz
|
| 135 |
+
python -m idlelib.idle_test.test_xyz
|
| 136 |
+
|
| 137 |
+
The following runs all idle_test/test_*.py tests interactively.
|
| 138 |
+
|
| 139 |
+
>>> import unittest
|
| 140 |
+
>>> unittest.main('idlelib.idle_test', verbosity=2)
|
| 141 |
+
|
| 142 |
+
The following run all Idle tests at a command line. Option '-v' is the
|
| 143 |
+
same as 'verbosity=2'.
|
| 144 |
+
|
| 145 |
+
python -m unittest -v idlelib.idle_test
|
| 146 |
+
python -m test -v -ugui test_idle
|
| 147 |
+
python -m test.test_idle
|
| 148 |
+
|
| 149 |
+
The idle tests are 'discovered' by
|
| 150 |
+
idlelib.idle_test.__init__.load_tests, which is also imported into
|
| 151 |
+
test.test_idle. Normally, neither file should be changed when working on
|
| 152 |
+
individual test modules. The third command runs unittest indirectly
|
| 153 |
+
through regrtest. The same happens when the entire test suite is run
|
| 154 |
+
with 'python -m test'. So that command must work for buildbots to stay
|
| 155 |
+
green. Idle tests must not disturb the environment in a way that makes
|
| 156 |
+
other tests fail (issue 18081).
|
| 157 |
+
|
| 158 |
+
To run an individual Testcase or test method, extend the dotted name
|
| 159 |
+
given to unittest on the command line or use the test -m option. The
|
| 160 |
+
latter allows use of other regrtest options. When using the latter,
|
| 161 |
+
all components of the pattern must be present, but any can be replaced
|
| 162 |
+
by '*'.
|
| 163 |
+
|
| 164 |
+
python -m unittest -v idlelib.idle_test.test_xyz.Test_case.test_meth
|
| 165 |
+
python -m test -m idlelib.idle_test.text_xyz.Test_case.test_meth test_idle
|
| 166 |
+
|
| 167 |
+
The test suite can be run in an IDLE user process from Shell.
|
| 168 |
+
>>> import test.autotest # Issue 25588, 2017/10/13, 3.6.4, 3.7.0a2.
|
| 169 |
+
There are currently failures not usually present, and this does not
|
| 170 |
+
work when run from the editor.
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
4. Human-mediated Tests
|
| 174 |
+
|
| 175 |
+
Human-mediated tests are widget tests that cannot be automated but need
|
| 176 |
+
human verification. They are contained in idlelib/idle_test/htest.py,
|
| 177 |
+
which has instructions. (Some modules need an auxiliary function,
|
| 178 |
+
identified with "# htest # on the header line.) The set is about
|
| 179 |
+
complete, though some tests need improvement. To run all htests, run the
|
| 180 |
+
htest file from an editor or from the command line with:
|
| 181 |
+
|
| 182 |
+
python -m idlelib.idle_test.htest
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
5. Test Coverage
|
| 186 |
+
|
| 187 |
+
Install the coverage package into your Python 3.6 site-packages
|
| 188 |
+
directory. (Its exact location depends on the OS).
|
| 189 |
+
> python3 -m pip install coverage
|
| 190 |
+
(On Windows, replace 'python3 with 'py -3.6' or perhaps just 'python'.)
|
| 191 |
+
|
| 192 |
+
The problem with running coverage with repository python is that
|
| 193 |
+
coverage uses absolute imports for its submodules, hence it needs to be
|
| 194 |
+
in a directory in sys.path. One solution: copy the package to the
|
| 195 |
+
directory containing the cpython repository. Call it 'dev'. Then run
|
| 196 |
+
coverage either directly or from a script in that directory so that
|
| 197 |
+
'dev' is prepended to sys.path.
|
| 198 |
+
|
| 199 |
+
Either edit or add dev/.coveragerc so it looks something like this.
|
| 200 |
+
---
|
| 201 |
+
# .coveragerc sets coverage options.
|
| 202 |
+
[run]
|
| 203 |
+
branch = True
|
| 204 |
+
|
| 205 |
+
[report]
|
| 206 |
+
# Regexes for lines to exclude from consideration
|
| 207 |
+
exclude_lines =
|
| 208 |
+
# Don't complain if non-runnable code isn't run:
|
| 209 |
+
if 0:
|
| 210 |
+
if __name__ == .__main__.:
|
| 211 |
+
|
| 212 |
+
.*# htest #
|
| 213 |
+
if not _utest:
|
| 214 |
+
if _htest:
|
| 215 |
+
---
|
| 216 |
+
The additions for IDLE are 'branch = True', to test coverage both ways,
|
| 217 |
+
and the last three exclude lines, to exclude things peculiar to IDLE
|
| 218 |
+
that are not executed during tests.
|
| 219 |
+
|
| 220 |
+
A script like the following cover.bat (for Windows) is very handy.
|
| 221 |
+
---
|
| 222 |
+
@echo off
|
| 223 |
+
rem Usage: cover filename [test_ suffix] # proper case required by coverage
|
| 224 |
+
rem filename without .py, 2nd parameter if test is not test_filename
|
| 225 |
+
setlocal
|
| 226 |
+
set py=f:\dev\3x\pcbuild\win32\python_d.exe
|
| 227 |
+
set src=idlelib.%1
|
| 228 |
+
if "%2" EQU "" set tst=f:/dev/3x/Lib/idlelib/idle_test/test_%1.py
|
| 229 |
+
if "%2" NEQ "" set tst=f:/dev/ex/Lib/idlelib/idle_test/test_%2.py
|
| 230 |
+
|
| 231 |
+
%py% -m coverage run --pylib --source=%src% %tst%
|
| 232 |
+
%py% -m coverage report --show-missing
|
| 233 |
+
%py% -m coverage html
|
| 234 |
+
start htmlcov\3x_Lib_idlelib_%1_py.html
|
| 235 |
+
rem Above opens new report; htmlcov\index.html displays report index
|
| 236 |
+
---
|
| 237 |
+
The second parameter was added for tests of module x not named test_x.
|
| 238 |
+
(There were several before modules were renamed, now only one is left.)
|
llava/lib/python3.10/idlelib/idle_test/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''idlelib.idle_test is a private implementation of test.test_idle,
|
| 2 |
+
which tests the IDLE application as part of the stdlib test suite.
|
| 3 |
+
Run IDLE tests alone with "python -m test.test_idle".
|
| 4 |
+
Starting with Python 3.6, IDLE requires tcl/tk 8.5 or later.
|
| 5 |
+
|
| 6 |
+
This package and its contained modules are subject to change and
|
| 7 |
+
any direct use is at your own risk.
|
| 8 |
+
'''
|
| 9 |
+
from os.path import dirname
|
| 10 |
+
|
| 11 |
+
def load_tests(loader, standard_tests, pattern):
|
| 12 |
+
this_dir = dirname(__file__)
|
| 13 |
+
top_dir = dirname(dirname(this_dir))
|
| 14 |
+
package_tests = loader.discover(start_dir=this_dir, pattern='test*.py',
|
| 15 |
+
top_level_dir=top_dir)
|
| 16 |
+
standard_tests.addTests(package_tests)
|
| 17 |
+
return standard_tests
|
llava/lib/python3.10/idlelib/idle_test/example_noext
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Classes that replace tkinter gui objects used by an object being tested.
|
| 2 |
+
|
| 3 |
+
A gui object is anything with a master or parent parameter, which is
|
| 4 |
+
typically required in spite of what the doc strings say.
|
| 5 |
+
"""
|
| 6 |
+
import re
|
| 7 |
+
from _tkinter import TclError
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class Event:
|
| 11 |
+
'''Minimal mock with attributes for testing event handlers.
|
| 12 |
+
|
| 13 |
+
This is not a gui object, but is used as an argument for callbacks
|
| 14 |
+
that access attributes of the event passed. If a callback ignores
|
| 15 |
+
the event, other than the fact that is happened, pass 'event'.
|
| 16 |
+
|
| 17 |
+
Keyboard, mouse, window, and other sources generate Event instances.
|
| 18 |
+
Event instances have the following attributes: serial (number of
|
| 19 |
+
event), time (of event), type (of event as number), widget (in which
|
| 20 |
+
event occurred), and x,y (position of mouse). There are other
|
| 21 |
+
attributes for specific events, such as keycode for key events.
|
| 22 |
+
tkinter.Event.__doc__ has more but is still not complete.
|
| 23 |
+
'''
|
| 24 |
+
def __init__(self, **kwds):
|
| 25 |
+
"Create event with attributes needed for test"
|
| 26 |
+
self.__dict__.update(kwds)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class Var:
|
| 30 |
+
"Use for String/Int/BooleanVar: incomplete"
|
| 31 |
+
def __init__(self, master=None, value=None, name=None):
|
| 32 |
+
self.master = master
|
| 33 |
+
self.value = value
|
| 34 |
+
self.name = name
|
| 35 |
+
def set(self, value):
|
| 36 |
+
self.value = value
|
| 37 |
+
def get(self):
|
| 38 |
+
return self.value
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class Mbox_func:
|
| 42 |
+
"""Generic mock for messagebox functions, which all have the same signature.
|
| 43 |
+
|
| 44 |
+
Instead of displaying a message box, the mock's call method saves the
|
| 45 |
+
arguments as instance attributes, which test functions can then examine.
|
| 46 |
+
The test can set the result returned to ask function
|
| 47 |
+
"""
|
| 48 |
+
def __init__(self, result=None):
|
| 49 |
+
self.result = result # Return None for all show funcs
|
| 50 |
+
def __call__(self, title, message, *args, **kwds):
|
| 51 |
+
# Save all args for possible examination by tester
|
| 52 |
+
self.title = title
|
| 53 |
+
self.message = message
|
| 54 |
+
self.args = args
|
| 55 |
+
self.kwds = kwds
|
| 56 |
+
return self.result # Set by tester for ask functions
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class Mbox:
|
| 60 |
+
"""Mock for tkinter.messagebox with an Mbox_func for each function.
|
| 61 |
+
|
| 62 |
+
Example usage in test_module.py for testing functions in module.py:
|
| 63 |
+
---
|
| 64 |
+
from idlelib.idle_test.mock_tk import Mbox
|
| 65 |
+
import module
|
| 66 |
+
|
| 67 |
+
orig_mbox = module.messagebox
|
| 68 |
+
showerror = Mbox.showerror # example, for attribute access in test methods
|
| 69 |
+
|
| 70 |
+
class Test(unittest.TestCase):
|
| 71 |
+
|
| 72 |
+
@classmethod
|
| 73 |
+
def setUpClass(cls):
|
| 74 |
+
module.messagebox = Mbox
|
| 75 |
+
|
| 76 |
+
@classmethod
|
| 77 |
+
def tearDownClass(cls):
|
| 78 |
+
module.messagebox = orig_mbox
|
| 79 |
+
---
|
| 80 |
+
For 'ask' functions, set func.result return value before calling the method
|
| 81 |
+
that uses the message function. When messagebox functions are the
|
| 82 |
+
only GUI calls in a method, this replacement makes the method GUI-free,
|
| 83 |
+
"""
|
| 84 |
+
askokcancel = Mbox_func() # True or False
|
| 85 |
+
askquestion = Mbox_func() # 'yes' or 'no'
|
| 86 |
+
askretrycancel = Mbox_func() # True or False
|
| 87 |
+
askyesno = Mbox_func() # True or False
|
| 88 |
+
askyesnocancel = Mbox_func() # True, False, or None
|
| 89 |
+
showerror = Mbox_func() # None
|
| 90 |
+
showinfo = Mbox_func() # None
|
| 91 |
+
showwarning = Mbox_func() # None
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class Text:
|
| 95 |
+
"""A semi-functional non-gui replacement for tkinter.Text text editors.
|
| 96 |
+
|
| 97 |
+
The mock's data model is that a text is a list of \n-terminated lines.
|
| 98 |
+
The mock adds an empty string at the beginning of the list so that the
|
| 99 |
+
index of actual lines start at 1, as with Tk. The methods never see this.
|
| 100 |
+
Tk initializes files with a terminal \n that cannot be deleted. It is
|
| 101 |
+
invisible in the sense that one cannot move the cursor beyond it.
|
| 102 |
+
|
| 103 |
+
This class is only tested (and valid) with strings of ascii chars.
|
| 104 |
+
For testing, we are not concerned with Tk Text's treatment of,
|
| 105 |
+
for instance, 0-width characters or character + accent.
|
| 106 |
+
"""
|
| 107 |
+
def __init__(self, master=None, cnf={}, **kw):
|
| 108 |
+
'''Initialize mock, non-gui, text-only Text widget.
|
| 109 |
+
|
| 110 |
+
At present, all args are ignored. Almost all affect visual behavior.
|
| 111 |
+
There are just a few Text-only options that affect text behavior.
|
| 112 |
+
'''
|
| 113 |
+
self.data = ['', '\n']
|
| 114 |
+
|
| 115 |
+
def index(self, index):
|
| 116 |
+
"Return string version of index decoded according to current text."
|
| 117 |
+
return "%s.%s" % self._decode(index, endflag=1)
|
| 118 |
+
|
| 119 |
+
def _decode(self, index, endflag=0):
|
| 120 |
+
"""Return a (line, char) tuple of int indexes into self.data.
|
| 121 |
+
|
| 122 |
+
This implements .index without converting the result back to a string.
|
| 123 |
+
The result is constrained by the number of lines and linelengths of
|
| 124 |
+
self.data. For many indexes, the result is initially (1, 0).
|
| 125 |
+
|
| 126 |
+
The input index may have any of several possible forms:
|
| 127 |
+
* line.char float: converted to 'line.char' string;
|
| 128 |
+
* 'line.char' string, where line and char are decimal integers;
|
| 129 |
+
* 'line.char lineend', where lineend='lineend' (and char is ignored);
|
| 130 |
+
* 'line.end', where end='end' (same as above);
|
| 131 |
+
* 'insert', the positions before terminal \n;
|
| 132 |
+
* 'end', whose meaning depends on the endflag passed to ._endex.
|
| 133 |
+
* 'sel.first' or 'sel.last', where sel is a tag -- not implemented.
|
| 134 |
+
"""
|
| 135 |
+
if isinstance(index, (float, bytes)):
|
| 136 |
+
index = str(index)
|
| 137 |
+
try:
|
| 138 |
+
index=index.lower()
|
| 139 |
+
except AttributeError:
|
| 140 |
+
raise TclError('bad text index "%s"' % index) from None
|
| 141 |
+
|
| 142 |
+
lastline = len(self.data) - 1 # same as number of text lines
|
| 143 |
+
if index == 'insert':
|
| 144 |
+
return lastline, len(self.data[lastline]) - 1
|
| 145 |
+
elif index == 'end':
|
| 146 |
+
return self._endex(endflag)
|
| 147 |
+
|
| 148 |
+
line, char = index.split('.')
|
| 149 |
+
line = int(line)
|
| 150 |
+
|
| 151 |
+
# Out of bounds line becomes first or last ('end') index
|
| 152 |
+
if line < 1:
|
| 153 |
+
return 1, 0
|
| 154 |
+
elif line > lastline:
|
| 155 |
+
return self._endex(endflag)
|
| 156 |
+
|
| 157 |
+
linelength = len(self.data[line]) -1 # position before/at \n
|
| 158 |
+
if char.endswith(' lineend') or char == 'end':
|
| 159 |
+
return line, linelength
|
| 160 |
+
# Tk requires that ignored chars before ' lineend' be valid int
|
| 161 |
+
if m := re.fullmatch(r'end-(\d*)c', char, re.A): # Used by hyperparser.
|
| 162 |
+
return line, linelength - int(m.group(1))
|
| 163 |
+
|
| 164 |
+
# Out of bounds char becomes first or last index of line
|
| 165 |
+
char = int(char)
|
| 166 |
+
if char < 0:
|
| 167 |
+
char = 0
|
| 168 |
+
elif char > linelength:
|
| 169 |
+
char = linelength
|
| 170 |
+
return line, char
|
| 171 |
+
|
| 172 |
+
def _endex(self, endflag):
|
| 173 |
+
'''Return position for 'end' or line overflow corresponding to endflag.
|
| 174 |
+
|
| 175 |
+
-1: position before terminal \n; for .insert(), .delete
|
| 176 |
+
0: position after terminal \n; for .get, .delete index 1
|
| 177 |
+
1: same viewed as beginning of non-existent next line (for .index)
|
| 178 |
+
'''
|
| 179 |
+
n = len(self.data)
|
| 180 |
+
if endflag == 1:
|
| 181 |
+
return n, 0
|
| 182 |
+
else:
|
| 183 |
+
n -= 1
|
| 184 |
+
return n, len(self.data[n]) + endflag
|
| 185 |
+
|
| 186 |
+
def insert(self, index, chars):
|
| 187 |
+
"Insert chars before the character at index."
|
| 188 |
+
|
| 189 |
+
if not chars: # ''.splitlines() is [], not ['']
|
| 190 |
+
return
|
| 191 |
+
chars = chars.splitlines(True)
|
| 192 |
+
if chars[-1][-1] == '\n':
|
| 193 |
+
chars.append('')
|
| 194 |
+
line, char = self._decode(index, -1)
|
| 195 |
+
before = self.data[line][:char]
|
| 196 |
+
after = self.data[line][char:]
|
| 197 |
+
self.data[line] = before + chars[0]
|
| 198 |
+
self.data[line+1:line+1] = chars[1:]
|
| 199 |
+
self.data[line+len(chars)-1] += after
|
| 200 |
+
|
| 201 |
+
def get(self, index1, index2=None):
|
| 202 |
+
"Return slice from index1 to index2 (default is 'index1+1')."
|
| 203 |
+
|
| 204 |
+
startline, startchar = self._decode(index1)
|
| 205 |
+
if index2 is None:
|
| 206 |
+
endline, endchar = startline, startchar+1
|
| 207 |
+
else:
|
| 208 |
+
endline, endchar = self._decode(index2)
|
| 209 |
+
|
| 210 |
+
if startline == endline:
|
| 211 |
+
return self.data[startline][startchar:endchar]
|
| 212 |
+
else:
|
| 213 |
+
lines = [self.data[startline][startchar:]]
|
| 214 |
+
for i in range(startline+1, endline):
|
| 215 |
+
lines.append(self.data[i])
|
| 216 |
+
lines.append(self.data[endline][:endchar])
|
| 217 |
+
return ''.join(lines)
|
| 218 |
+
|
| 219 |
+
def delete(self, index1, index2=None):
|
| 220 |
+
'''Delete slice from index1 to index2 (default is 'index1+1').
|
| 221 |
+
|
| 222 |
+
Adjust default index2 ('index+1) for line ends.
|
| 223 |
+
Do not delete the terminal \n at the very end of self.data ([-1][-1]).
|
| 224 |
+
'''
|
| 225 |
+
startline, startchar = self._decode(index1, -1)
|
| 226 |
+
if index2 is None:
|
| 227 |
+
if startchar < len(self.data[startline])-1:
|
| 228 |
+
# not deleting \n
|
| 229 |
+
endline, endchar = startline, startchar+1
|
| 230 |
+
elif startline < len(self.data) - 1:
|
| 231 |
+
# deleting non-terminal \n, convert 'index1+1 to start of next line
|
| 232 |
+
endline, endchar = startline+1, 0
|
| 233 |
+
else:
|
| 234 |
+
# do not delete terminal \n if index1 == 'insert'
|
| 235 |
+
return
|
| 236 |
+
else:
|
| 237 |
+
endline, endchar = self._decode(index2, -1)
|
| 238 |
+
# restricting end position to insert position excludes terminal \n
|
| 239 |
+
|
| 240 |
+
if startline == endline and startchar < endchar:
|
| 241 |
+
self.data[startline] = self.data[startline][:startchar] + \
|
| 242 |
+
self.data[startline][endchar:]
|
| 243 |
+
elif startline < endline:
|
| 244 |
+
self.data[startline] = self.data[startline][:startchar] + \
|
| 245 |
+
self.data[endline][endchar:]
|
| 246 |
+
startline += 1
|
| 247 |
+
for i in range(startline, endline+1):
|
| 248 |
+
del self.data[startline]
|
| 249 |
+
|
| 250 |
+
def compare(self, index1, op, index2):
|
| 251 |
+
line1, char1 = self._decode(index1)
|
| 252 |
+
line2, char2 = self._decode(index2)
|
| 253 |
+
if op == '<':
|
| 254 |
+
return line1 < line2 or line1 == line2 and char1 < char2
|
| 255 |
+
elif op == '<=':
|
| 256 |
+
return line1 < line2 or line1 == line2 and char1 <= char2
|
| 257 |
+
elif op == '>':
|
| 258 |
+
return line1 > line2 or line1 == line2 and char1 > char2
|
| 259 |
+
elif op == '>=':
|
| 260 |
+
return line1 > line2 or line1 == line2 and char1 >= char2
|
| 261 |
+
elif op == '==':
|
| 262 |
+
return line1 == line2 and char1 == char2
|
| 263 |
+
elif op == '!=':
|
| 264 |
+
return line1 != line2 or char1 != char2
|
| 265 |
+
else:
|
| 266 |
+
raise TclError('''bad comparison operator "%s": '''
|
| 267 |
+
'''must be <, <=, ==, >=, >, or !=''' % op)
|
| 268 |
+
|
| 269 |
+
# The following Text methods normally do something and return None.
|
| 270 |
+
# Whether doing nothing is sufficient for a test will depend on the test.
|
| 271 |
+
|
| 272 |
+
def mark_set(self, name, index):
|
| 273 |
+
"Set mark *name* before the character at index."
|
| 274 |
+
pass
|
| 275 |
+
|
| 276 |
+
def mark_unset(self, *markNames):
|
| 277 |
+
"Delete all marks in markNames."
|
| 278 |
+
|
| 279 |
+
def tag_remove(self, tagName, index1, index2=None):
|
| 280 |
+
"Remove tag tagName from all characters between index1 and index2."
|
| 281 |
+
pass
|
| 282 |
+
|
| 283 |
+
# The following Text methods affect the graphics screen and return None.
|
| 284 |
+
# Doing nothing should always be sufficient for tests.
|
| 285 |
+
|
| 286 |
+
def scan_dragto(self, x, y):
|
| 287 |
+
"Adjust the view of the text according to scan_mark"
|
| 288 |
+
|
| 289 |
+
def scan_mark(self, x, y):
|
| 290 |
+
"Remember the current X, Y coordinates."
|
| 291 |
+
|
| 292 |
+
def see(self, index):
|
| 293 |
+
"Scroll screen to make the character at INDEX is visible."
|
| 294 |
+
pass
|
| 295 |
+
|
| 296 |
+
# The following is a Misc method inherited by Text.
|
| 297 |
+
# It should properly go in a Misc mock, but is included here for now.
|
| 298 |
+
|
| 299 |
+
def bind(sequence=None, func=None, add=None):
|
| 300 |
+
"Bind to this widget at event sequence a call to function func."
|
| 301 |
+
pass
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
class Entry:
|
| 305 |
+
"Mock for tkinter.Entry."
|
| 306 |
+
def focus_set(self):
|
| 307 |
+
pass
|
llava/lib/python3.10/idlelib/idle_test/test_browser.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test browser, coverage 90%."
|
| 2 |
+
|
| 3 |
+
from idlelib import browser
|
| 4 |
+
from test.support import requires
|
| 5 |
+
import unittest
|
| 6 |
+
from unittest import mock
|
| 7 |
+
from idlelib.idle_test.mock_idle import Func
|
| 8 |
+
from idlelib.util import py_extensions
|
| 9 |
+
|
| 10 |
+
from collections import deque
|
| 11 |
+
import os.path
|
| 12 |
+
import pyclbr
|
| 13 |
+
from tkinter import Tk
|
| 14 |
+
|
| 15 |
+
from idlelib.tree import TreeNode
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ModuleBrowserTest(unittest.TestCase):
|
| 19 |
+
|
| 20 |
+
@classmethod
|
| 21 |
+
def setUpClass(cls):
|
| 22 |
+
requires('gui')
|
| 23 |
+
cls.root = Tk()
|
| 24 |
+
cls.root.withdraw()
|
| 25 |
+
cls.mb = browser.ModuleBrowser(cls.root, __file__, _utest=True)
|
| 26 |
+
|
| 27 |
+
@classmethod
|
| 28 |
+
def tearDownClass(cls):
|
| 29 |
+
cls.mb.close()
|
| 30 |
+
cls.root.update_idletasks()
|
| 31 |
+
cls.root.destroy()
|
| 32 |
+
del cls.root, cls.mb
|
| 33 |
+
|
| 34 |
+
def test_init(self):
|
| 35 |
+
mb = self.mb
|
| 36 |
+
eq = self.assertEqual
|
| 37 |
+
eq(mb.path, __file__)
|
| 38 |
+
eq(pyclbr._modules, {})
|
| 39 |
+
self.assertIsInstance(mb.node, TreeNode)
|
| 40 |
+
self.assertIsNotNone(browser.file_open)
|
| 41 |
+
|
| 42 |
+
def test_settitle(self):
|
| 43 |
+
mb = self.mb
|
| 44 |
+
self.assertIn(os.path.basename(__file__), mb.top.title())
|
| 45 |
+
self.assertEqual(mb.top.iconname(), 'Module Browser')
|
| 46 |
+
|
| 47 |
+
def test_rootnode(self):
|
| 48 |
+
mb = self.mb
|
| 49 |
+
rn = mb.rootnode()
|
| 50 |
+
self.assertIsInstance(rn, browser.ModuleBrowserTreeItem)
|
| 51 |
+
|
| 52 |
+
def test_close(self):
|
| 53 |
+
mb = self.mb
|
| 54 |
+
mb.top.destroy = Func()
|
| 55 |
+
mb.node.destroy = Func()
|
| 56 |
+
mb.close()
|
| 57 |
+
self.assertTrue(mb.top.destroy.called)
|
| 58 |
+
self.assertTrue(mb.node.destroy.called)
|
| 59 |
+
del mb.top.destroy, mb.node.destroy
|
| 60 |
+
|
| 61 |
+
def test_is_browseable_extension(self):
|
| 62 |
+
path = "/path/to/file"
|
| 63 |
+
for ext in py_extensions:
|
| 64 |
+
with self.subTest(ext=ext):
|
| 65 |
+
filename = f'{path}{ext}'
|
| 66 |
+
actual = browser.is_browseable_extension(filename)
|
| 67 |
+
expected = ext not in browser.browseable_extension_blocklist
|
| 68 |
+
self.assertEqual(actual, expected)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# Nested tree same as in test_pyclbr.py except for supers on C0. C1.
|
| 72 |
+
mb = pyclbr
|
| 73 |
+
module, fname = 'test', 'test.py'
|
| 74 |
+
C0 = mb.Class(module, 'C0', ['base'], fname, 1, end_lineno=9)
|
| 75 |
+
F1 = mb._nest_function(C0, 'F1', 3, 5)
|
| 76 |
+
C1 = mb._nest_class(C0, 'C1', 6, 9, [''])
|
| 77 |
+
C2 = mb._nest_class(C1, 'C2', 7, 9)
|
| 78 |
+
F3 = mb._nest_function(C2, 'F3', 9, 9)
|
| 79 |
+
f0 = mb.Function(module, 'f0', fname, 11, end_lineno=15)
|
| 80 |
+
f1 = mb._nest_function(f0, 'f1', 12, 14)
|
| 81 |
+
f2 = mb._nest_function(f1, 'f2', 13, 13)
|
| 82 |
+
c1 = mb._nest_class(f0, 'c1', 15, 15)
|
| 83 |
+
mock_pyclbr_tree = {'C0': C0, 'f0': f0}
|
| 84 |
+
|
| 85 |
+
# Adjust C0.name, C1.name so tests do not depend on order.
|
| 86 |
+
browser.transform_children(mock_pyclbr_tree, 'test') # C0(base)
|
| 87 |
+
browser.transform_children(C0.children) # C1()
|
| 88 |
+
|
| 89 |
+
# The class below checks that the calls above are correct
|
| 90 |
+
# and that duplicate calls have no effect.
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
class TransformChildrenTest(unittest.TestCase):
|
| 94 |
+
|
| 95 |
+
def test_transform_module_children(self):
|
| 96 |
+
eq = self.assertEqual
|
| 97 |
+
transform = browser.transform_children
|
| 98 |
+
# Parameter matches tree module.
|
| 99 |
+
tcl = list(transform(mock_pyclbr_tree, 'test'))
|
| 100 |
+
eq(tcl, [C0, f0])
|
| 101 |
+
eq(tcl[0].name, 'C0(base)')
|
| 102 |
+
eq(tcl[1].name, 'f0')
|
| 103 |
+
# Check that second call does not change suffix.
|
| 104 |
+
tcl = list(transform(mock_pyclbr_tree, 'test'))
|
| 105 |
+
eq(tcl[0].name, 'C0(base)')
|
| 106 |
+
# Nothing to traverse if parameter name isn't same as tree module.
|
| 107 |
+
tcl = list(transform(mock_pyclbr_tree, 'different name'))
|
| 108 |
+
eq(tcl, [])
|
| 109 |
+
|
| 110 |
+
def test_transform_node_children(self):
|
| 111 |
+
eq = self.assertEqual
|
| 112 |
+
transform = browser.transform_children
|
| 113 |
+
# Class with two children, one name altered.
|
| 114 |
+
tcl = list(transform(C0.children))
|
| 115 |
+
eq(tcl, [F1, C1])
|
| 116 |
+
eq(tcl[0].name, 'F1')
|
| 117 |
+
eq(tcl[1].name, 'C1()')
|
| 118 |
+
tcl = list(transform(C0.children))
|
| 119 |
+
eq(tcl[1].name, 'C1()')
|
| 120 |
+
# Function with two children.
|
| 121 |
+
eq(list(transform(f0.children)), [f1, c1])
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class ModuleBrowserTreeItemTest(unittest.TestCase):
|
| 125 |
+
|
| 126 |
+
@classmethod
|
| 127 |
+
def setUpClass(cls):
|
| 128 |
+
cls.mbt = browser.ModuleBrowserTreeItem(fname)
|
| 129 |
+
|
| 130 |
+
def test_init(self):
|
| 131 |
+
self.assertEqual(self.mbt.file, fname)
|
| 132 |
+
|
| 133 |
+
def test_gettext(self):
|
| 134 |
+
self.assertEqual(self.mbt.GetText(), fname)
|
| 135 |
+
|
| 136 |
+
def test_geticonname(self):
|
| 137 |
+
self.assertEqual(self.mbt.GetIconName(), 'python')
|
| 138 |
+
|
| 139 |
+
def test_isexpandable(self):
|
| 140 |
+
self.assertTrue(self.mbt.IsExpandable())
|
| 141 |
+
|
| 142 |
+
def test_listchildren(self):
|
| 143 |
+
save_rex = browser.pyclbr.readmodule_ex
|
| 144 |
+
save_tc = browser.transform_children
|
| 145 |
+
browser.pyclbr.readmodule_ex = Func(result=mock_pyclbr_tree)
|
| 146 |
+
browser.transform_children = Func(result=[f0, C0])
|
| 147 |
+
try:
|
| 148 |
+
self.assertEqual(self.mbt.listchildren(), [f0, C0])
|
| 149 |
+
finally:
|
| 150 |
+
browser.pyclbr.readmodule_ex = save_rex
|
| 151 |
+
browser.transform_children = save_tc
|
| 152 |
+
|
| 153 |
+
def test_getsublist(self):
|
| 154 |
+
mbt = self.mbt
|
| 155 |
+
mbt.listchildren = Func(result=[f0, C0])
|
| 156 |
+
sub0, sub1 = mbt.GetSubList()
|
| 157 |
+
del mbt.listchildren
|
| 158 |
+
self.assertIsInstance(sub0, browser.ChildBrowserTreeItem)
|
| 159 |
+
self.assertIsInstance(sub1, browser.ChildBrowserTreeItem)
|
| 160 |
+
self.assertEqual(sub0.name, 'f0')
|
| 161 |
+
self.assertEqual(sub1.name, 'C0(base)')
|
| 162 |
+
|
| 163 |
+
@mock.patch('idlelib.browser.file_open')
|
| 164 |
+
def test_ondoubleclick(self, fopen):
|
| 165 |
+
mbt = self.mbt
|
| 166 |
+
|
| 167 |
+
with mock.patch('os.path.exists', return_value=False):
|
| 168 |
+
mbt.OnDoubleClick()
|
| 169 |
+
fopen.assert_not_called()
|
| 170 |
+
|
| 171 |
+
with mock.patch('os.path.exists', return_value=True):
|
| 172 |
+
mbt.OnDoubleClick()
|
| 173 |
+
fopen.assert_called_once_with(fname)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class ChildBrowserTreeItemTest(unittest.TestCase):
|
| 177 |
+
|
| 178 |
+
@classmethod
|
| 179 |
+
def setUpClass(cls):
|
| 180 |
+
CBT = browser.ChildBrowserTreeItem
|
| 181 |
+
cls.cbt_f1 = CBT(f1)
|
| 182 |
+
cls.cbt_C1 = CBT(C1)
|
| 183 |
+
cls.cbt_F1 = CBT(F1)
|
| 184 |
+
|
| 185 |
+
@classmethod
|
| 186 |
+
def tearDownClass(cls):
|
| 187 |
+
del cls.cbt_C1, cls.cbt_f1, cls.cbt_F1
|
| 188 |
+
|
| 189 |
+
def test_init(self):
|
| 190 |
+
eq = self.assertEqual
|
| 191 |
+
eq(self.cbt_C1.name, 'C1()')
|
| 192 |
+
self.assertFalse(self.cbt_C1.isfunction)
|
| 193 |
+
eq(self.cbt_f1.name, 'f1')
|
| 194 |
+
self.assertTrue(self.cbt_f1.isfunction)
|
| 195 |
+
|
| 196 |
+
def test_gettext(self):
|
| 197 |
+
self.assertEqual(self.cbt_C1.GetText(), 'class C1()')
|
| 198 |
+
self.assertEqual(self.cbt_f1.GetText(), 'def f1(...)')
|
| 199 |
+
|
| 200 |
+
def test_geticonname(self):
|
| 201 |
+
self.assertEqual(self.cbt_C1.GetIconName(), 'folder')
|
| 202 |
+
self.assertEqual(self.cbt_f1.GetIconName(), 'python')
|
| 203 |
+
|
| 204 |
+
def test_isexpandable(self):
|
| 205 |
+
self.assertTrue(self.cbt_C1.IsExpandable())
|
| 206 |
+
self.assertTrue(self.cbt_f1.IsExpandable())
|
| 207 |
+
self.assertFalse(self.cbt_F1.IsExpandable())
|
| 208 |
+
|
| 209 |
+
def test_getsublist(self):
|
| 210 |
+
eq = self.assertEqual
|
| 211 |
+
CBT = browser.ChildBrowserTreeItem
|
| 212 |
+
|
| 213 |
+
f1sublist = self.cbt_f1.GetSubList()
|
| 214 |
+
self.assertIsInstance(f1sublist[0], CBT)
|
| 215 |
+
eq(len(f1sublist), 1)
|
| 216 |
+
eq(f1sublist[0].name, 'f2')
|
| 217 |
+
|
| 218 |
+
eq(self.cbt_F1.GetSubList(), [])
|
| 219 |
+
|
| 220 |
+
@mock.patch('idlelib.browser.file_open')
|
| 221 |
+
def test_ondoubleclick(self, fopen):
|
| 222 |
+
goto = fopen.return_value.gotoline = mock.Mock()
|
| 223 |
+
self.cbt_F1.OnDoubleClick()
|
| 224 |
+
fopen.assert_called()
|
| 225 |
+
goto.assert_called()
|
| 226 |
+
goto.assert_called_with(self.cbt_F1.obj.lineno)
|
| 227 |
+
# Failure test would have to raise OSError or AttributeError.
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
class NestedChildrenTest(unittest.TestCase):
|
| 231 |
+
"Test that all the nodes in a nested tree are added to the BrowserTree."
|
| 232 |
+
|
| 233 |
+
def test_nested(self):
|
| 234 |
+
queue = deque()
|
| 235 |
+
actual_names = []
|
| 236 |
+
# The tree items are processed in breadth first order.
|
| 237 |
+
# Verify that processing each sublist hits every node and
|
| 238 |
+
# in the right order.
|
| 239 |
+
expected_names = ['f0', 'C0(base)',
|
| 240 |
+
'f1', 'c1', 'F1', 'C1()',
|
| 241 |
+
'f2', 'C2',
|
| 242 |
+
'F3']
|
| 243 |
+
CBT = browser.ChildBrowserTreeItem
|
| 244 |
+
queue.extend((CBT(f0), CBT(C0)))
|
| 245 |
+
while queue:
|
| 246 |
+
cb = queue.popleft()
|
| 247 |
+
sublist = cb.GetSubList()
|
| 248 |
+
queue.extend(sublist)
|
| 249 |
+
self.assertIn(cb.name, cb.GetText())
|
| 250 |
+
self.assertIn(cb.GetIconName(), ('python', 'folder'))
|
| 251 |
+
self.assertIs(cb.IsExpandable(), sublist != [])
|
| 252 |
+
actual_names.append(cb.name)
|
| 253 |
+
self.assertEqual(actual_names, expected_names)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
if __name__ == '__main__':
|
| 257 |
+
unittest.main(verbosity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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test colorizer, coverage 99%."
|
| 2 |
+
from idlelib import colorizer
|
| 3 |
+
from test.support import requires
|
| 4 |
+
import unittest
|
| 5 |
+
from unittest import mock
|
| 6 |
+
from idlelib.idle_test.tkinter_testing_utils import run_in_tk_mainloop
|
| 7 |
+
|
| 8 |
+
from functools import partial
|
| 9 |
+
import textwrap
|
| 10 |
+
from tkinter import Tk, Text
|
| 11 |
+
from idlelib import config
|
| 12 |
+
from idlelib.percolator import Percolator
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
usercfg = colorizer.idleConf.userCfg
|
| 16 |
+
testcfg = {
|
| 17 |
+
'main': config.IdleUserConfParser(''),
|
| 18 |
+
'highlight': config.IdleUserConfParser(''),
|
| 19 |
+
'keys': config.IdleUserConfParser(''),
|
| 20 |
+
'extensions': config.IdleUserConfParser(''),
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
source = textwrap.dedent("""\
|
| 24 |
+
if True: int ('1') # keyword, builtin, string, comment
|
| 25 |
+
elif False: print(0) # 'string' in comment
|
| 26 |
+
else: float(None) # if in comment
|
| 27 |
+
if iF + If + IF: 'keyword matching must respect case'
|
| 28 |
+
if'': x or'' # valid keyword-string no-space combinations
|
| 29 |
+
async def f(): await g()
|
| 30 |
+
# Strings should be entirely colored, including quotes.
|
| 31 |
+
'x', '''x''', "x", \"""x\"""
|
| 32 |
+
'abc\\
|
| 33 |
+
def'
|
| 34 |
+
'''abc\\
|
| 35 |
+
def'''
|
| 36 |
+
# All valid prefixes for unicode and byte strings should be colored.
|
| 37 |
+
r'x', u'x', R'x', U'x', f'x', F'x'
|
| 38 |
+
fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x'
|
| 39 |
+
b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x', rB'x',Rb'x',RB'x'
|
| 40 |
+
# Invalid combinations of legal characters should be half colored.
|
| 41 |
+
ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x'
|
| 42 |
+
match point:
|
| 43 |
+
case (x, 0) as _:
|
| 44 |
+
print(f"X={x}")
|
| 45 |
+
case [_, [_], "_",
|
| 46 |
+
_]:
|
| 47 |
+
pass
|
| 48 |
+
case _ if ("a" if _ else set()): pass
|
| 49 |
+
case _:
|
| 50 |
+
raise ValueError("Not a point _")
|
| 51 |
+
'''
|
| 52 |
+
case _:'''
|
| 53 |
+
"match x:"
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def setUpModule():
|
| 58 |
+
colorizer.idleConf.userCfg = testcfg
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def tearDownModule():
|
| 62 |
+
colorizer.idleConf.userCfg = usercfg
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class FunctionTest(unittest.TestCase):
|
| 66 |
+
|
| 67 |
+
def test_any(self):
|
| 68 |
+
self.assertEqual(colorizer.any('test', ('a', 'b', 'cd')),
|
| 69 |
+
'(?P<test>a|b|cd)')
|
| 70 |
+
|
| 71 |
+
def test_make_pat(self):
|
| 72 |
+
# Tested in more detail by testing prog.
|
| 73 |
+
self.assertTrue(colorizer.make_pat())
|
| 74 |
+
|
| 75 |
+
def test_prog(self):
|
| 76 |
+
prog = colorizer.prog
|
| 77 |
+
eq = self.assertEqual
|
| 78 |
+
line = 'def f():\n print("hello")\n'
|
| 79 |
+
m = prog.search(line)
|
| 80 |
+
eq(m.groupdict()['KEYWORD'], 'def')
|
| 81 |
+
m = prog.search(line, m.end())
|
| 82 |
+
eq(m.groupdict()['SYNC'], '\n')
|
| 83 |
+
m = prog.search(line, m.end())
|
| 84 |
+
eq(m.groupdict()['BUILTIN'], 'print')
|
| 85 |
+
m = prog.search(line, m.end())
|
| 86 |
+
eq(m.groupdict()['STRING'], '"hello"')
|
| 87 |
+
m = prog.search(line, m.end())
|
| 88 |
+
eq(m.groupdict()['SYNC'], '\n')
|
| 89 |
+
|
| 90 |
+
def test_idprog(self):
|
| 91 |
+
idprog = colorizer.idprog
|
| 92 |
+
m = idprog.match('nospace')
|
| 93 |
+
self.assertIsNone(m)
|
| 94 |
+
m = idprog.match(' space')
|
| 95 |
+
self.assertEqual(m.group(0), ' space')
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class ColorConfigTest(unittest.TestCase):
|
| 99 |
+
|
| 100 |
+
@classmethod
|
| 101 |
+
def setUpClass(cls):
|
| 102 |
+
requires('gui')
|
| 103 |
+
root = cls.root = Tk()
|
| 104 |
+
root.withdraw()
|
| 105 |
+
cls.text = Text(root)
|
| 106 |
+
|
| 107 |
+
@classmethod
|
| 108 |
+
def tearDownClass(cls):
|
| 109 |
+
del cls.text
|
| 110 |
+
cls.root.update_idletasks()
|
| 111 |
+
cls.root.destroy()
|
| 112 |
+
del cls.root
|
| 113 |
+
|
| 114 |
+
def test_color_config(self):
|
| 115 |
+
text = self.text
|
| 116 |
+
eq = self.assertEqual
|
| 117 |
+
colorizer.color_config(text)
|
| 118 |
+
# Uses IDLE Classic theme as default.
|
| 119 |
+
eq(text['background'], '#ffffff')
|
| 120 |
+
eq(text['foreground'], '#000000')
|
| 121 |
+
eq(text['selectbackground'], 'gray')
|
| 122 |
+
eq(text['selectforeground'], '#000000')
|
| 123 |
+
eq(text['insertbackground'], 'black')
|
| 124 |
+
eq(text['inactiveselectbackground'], 'gray')
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class ColorDelegatorInstantiationTest(unittest.TestCase):
|
| 128 |
+
|
| 129 |
+
@classmethod
|
| 130 |
+
def setUpClass(cls):
|
| 131 |
+
requires('gui')
|
| 132 |
+
root = cls.root = Tk()
|
| 133 |
+
root.withdraw()
|
| 134 |
+
cls.text = Text(root)
|
| 135 |
+
|
| 136 |
+
@classmethod
|
| 137 |
+
def tearDownClass(cls):
|
| 138 |
+
del cls.text
|
| 139 |
+
cls.root.update_idletasks()
|
| 140 |
+
cls.root.destroy()
|
| 141 |
+
del cls.root
|
| 142 |
+
|
| 143 |
+
def setUp(self):
|
| 144 |
+
self.color = colorizer.ColorDelegator()
|
| 145 |
+
|
| 146 |
+
def tearDown(self):
|
| 147 |
+
self.color.close()
|
| 148 |
+
self.text.delete('1.0', 'end')
|
| 149 |
+
self.color.resetcache()
|
| 150 |
+
del self.color
|
| 151 |
+
|
| 152 |
+
def test_init(self):
|
| 153 |
+
color = self.color
|
| 154 |
+
self.assertIsInstance(color, colorizer.ColorDelegator)
|
| 155 |
+
|
| 156 |
+
def test_init_state(self):
|
| 157 |
+
# init_state() is called during the instantiation of
|
| 158 |
+
# ColorDelegator in setUp().
|
| 159 |
+
color = self.color
|
| 160 |
+
self.assertIsNone(color.after_id)
|
| 161 |
+
self.assertTrue(color.allow_colorizing)
|
| 162 |
+
self.assertFalse(color.colorizing)
|
| 163 |
+
self.assertFalse(color.stop_colorizing)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
class ColorDelegatorTest(unittest.TestCase):
|
| 167 |
+
|
| 168 |
+
@classmethod
|
| 169 |
+
def setUpClass(cls):
|
| 170 |
+
requires('gui')
|
| 171 |
+
root = cls.root = Tk()
|
| 172 |
+
root.withdraw()
|
| 173 |
+
text = cls.text = Text(root)
|
| 174 |
+
cls.percolator = Percolator(text)
|
| 175 |
+
# Delegator stack = [Delegator(text)]
|
| 176 |
+
|
| 177 |
+
@classmethod
|
| 178 |
+
def tearDownClass(cls):
|
| 179 |
+
cls.percolator.close()
|
| 180 |
+
del cls.percolator, cls.text
|
| 181 |
+
cls.root.update_idletasks()
|
| 182 |
+
cls.root.destroy()
|
| 183 |
+
del cls.root
|
| 184 |
+
|
| 185 |
+
def setUp(self):
|
| 186 |
+
self.color = colorizer.ColorDelegator()
|
| 187 |
+
self.percolator.insertfilter(self.color)
|
| 188 |
+
# Calls color.setdelegate(Delegator(text)).
|
| 189 |
+
|
| 190 |
+
def tearDown(self):
|
| 191 |
+
self.color.close()
|
| 192 |
+
self.percolator.removefilter(self.color)
|
| 193 |
+
self.text.delete('1.0', 'end')
|
| 194 |
+
self.color.resetcache()
|
| 195 |
+
del self.color
|
| 196 |
+
|
| 197 |
+
def test_setdelegate(self):
|
| 198 |
+
# Called in setUp when filter is attached to percolator.
|
| 199 |
+
color = self.color
|
| 200 |
+
self.assertIsInstance(color.delegate, colorizer.Delegator)
|
| 201 |
+
# It is too late to mock notify_range, so test side effect.
|
| 202 |
+
self.assertEqual(self.root.tk.call(
|
| 203 |
+
'after', 'info', color.after_id)[1], 'timer')
|
| 204 |
+
|
| 205 |
+
def test_LoadTagDefs(self):
|
| 206 |
+
highlight = partial(config.idleConf.GetHighlight, theme='IDLE Classic')
|
| 207 |
+
for tag, colors in self.color.tagdefs.items():
|
| 208 |
+
with self.subTest(tag=tag):
|
| 209 |
+
self.assertIn('background', colors)
|
| 210 |
+
self.assertIn('foreground', colors)
|
| 211 |
+
if tag not in ('SYNC', 'TODO'):
|
| 212 |
+
self.assertEqual(colors, highlight(element=tag.lower()))
|
| 213 |
+
|
| 214 |
+
def test_config_colors(self):
|
| 215 |
+
text = self.text
|
| 216 |
+
highlight = partial(config.idleConf.GetHighlight, theme='IDLE Classic')
|
| 217 |
+
for tag in self.color.tagdefs:
|
| 218 |
+
for plane in ('background', 'foreground'):
|
| 219 |
+
with self.subTest(tag=tag, plane=plane):
|
| 220 |
+
if tag in ('SYNC', 'TODO'):
|
| 221 |
+
self.assertEqual(text.tag_cget(tag, plane), '')
|
| 222 |
+
else:
|
| 223 |
+
self.assertEqual(text.tag_cget(tag, plane),
|
| 224 |
+
highlight(element=tag.lower())[plane])
|
| 225 |
+
# 'sel' is marked as the highest priority.
|
| 226 |
+
self.assertEqual(text.tag_names()[-1], 'sel')
|
| 227 |
+
|
| 228 |
+
@mock.patch.object(colorizer.ColorDelegator, 'notify_range')
|
| 229 |
+
def test_insert(self, mock_notify):
|
| 230 |
+
text = self.text
|
| 231 |
+
# Initial text.
|
| 232 |
+
text.insert('insert', 'foo')
|
| 233 |
+
self.assertEqual(text.get('1.0', 'end'), 'foo\n')
|
| 234 |
+
mock_notify.assert_called_with('1.0', '1.0+3c')
|
| 235 |
+
# Additional text.
|
| 236 |
+
text.insert('insert', 'barbaz')
|
| 237 |
+
self.assertEqual(text.get('1.0', 'end'), 'foobarbaz\n')
|
| 238 |
+
mock_notify.assert_called_with('1.3', '1.3+6c')
|
| 239 |
+
|
| 240 |
+
@mock.patch.object(colorizer.ColorDelegator, 'notify_range')
|
| 241 |
+
def test_delete(self, mock_notify):
|
| 242 |
+
text = self.text
|
| 243 |
+
# Initialize text.
|
| 244 |
+
text.insert('insert', 'abcdefghi')
|
| 245 |
+
self.assertEqual(text.get('1.0', 'end'), 'abcdefghi\n')
|
| 246 |
+
# Delete single character.
|
| 247 |
+
text.delete('1.7')
|
| 248 |
+
self.assertEqual(text.get('1.0', 'end'), 'abcdefgi\n')
|
| 249 |
+
mock_notify.assert_called_with('1.7')
|
| 250 |
+
# Delete multiple characters.
|
| 251 |
+
text.delete('1.3', '1.6')
|
| 252 |
+
self.assertEqual(text.get('1.0', 'end'), 'abcgi\n')
|
| 253 |
+
mock_notify.assert_called_with('1.3')
|
| 254 |
+
|
| 255 |
+
def test_notify_range(self):
|
| 256 |
+
text = self.text
|
| 257 |
+
color = self.color
|
| 258 |
+
eq = self.assertEqual
|
| 259 |
+
|
| 260 |
+
# Colorizing already scheduled.
|
| 261 |
+
save_id = color.after_id
|
| 262 |
+
eq(self.root.tk.call('after', 'info', save_id)[1], 'timer')
|
| 263 |
+
self.assertFalse(color.colorizing)
|
| 264 |
+
self.assertFalse(color.stop_colorizing)
|
| 265 |
+
self.assertTrue(color.allow_colorizing)
|
| 266 |
+
|
| 267 |
+
# Coloring scheduled and colorizing in progress.
|
| 268 |
+
color.colorizing = True
|
| 269 |
+
color.notify_range('1.0', 'end')
|
| 270 |
+
self.assertFalse(color.stop_colorizing)
|
| 271 |
+
eq(color.after_id, save_id)
|
| 272 |
+
|
| 273 |
+
# No colorizing scheduled and colorizing in progress.
|
| 274 |
+
text.after_cancel(save_id)
|
| 275 |
+
color.after_id = None
|
| 276 |
+
color.notify_range('1.0', '1.0+3c')
|
| 277 |
+
self.assertTrue(color.stop_colorizing)
|
| 278 |
+
self.assertIsNotNone(color.after_id)
|
| 279 |
+
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer')
|
| 280 |
+
# New event scheduled.
|
| 281 |
+
self.assertNotEqual(color.after_id, save_id)
|
| 282 |
+
|
| 283 |
+
# No colorizing scheduled and colorizing off.
|
| 284 |
+
text.after_cancel(color.after_id)
|
| 285 |
+
color.after_id = None
|
| 286 |
+
color.allow_colorizing = False
|
| 287 |
+
color.notify_range('1.4', '1.4+10c')
|
| 288 |
+
# Nothing scheduled when colorizing is off.
|
| 289 |
+
self.assertIsNone(color.after_id)
|
| 290 |
+
|
| 291 |
+
def test_toggle_colorize_event(self):
|
| 292 |
+
color = self.color
|
| 293 |
+
eq = self.assertEqual
|
| 294 |
+
|
| 295 |
+
# Starts with colorizing allowed and scheduled.
|
| 296 |
+
self.assertFalse(color.colorizing)
|
| 297 |
+
self.assertFalse(color.stop_colorizing)
|
| 298 |
+
self.assertTrue(color.allow_colorizing)
|
| 299 |
+
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer')
|
| 300 |
+
|
| 301 |
+
# Toggle colorizing off.
|
| 302 |
+
color.toggle_colorize_event()
|
| 303 |
+
self.assertIsNone(color.after_id)
|
| 304 |
+
self.assertFalse(color.colorizing)
|
| 305 |
+
self.assertFalse(color.stop_colorizing)
|
| 306 |
+
self.assertFalse(color.allow_colorizing)
|
| 307 |
+
|
| 308 |
+
# Toggle on while colorizing in progress (doesn't add timer).
|
| 309 |
+
color.colorizing = True
|
| 310 |
+
color.toggle_colorize_event()
|
| 311 |
+
self.assertIsNone(color.after_id)
|
| 312 |
+
self.assertTrue(color.colorizing)
|
| 313 |
+
self.assertFalse(color.stop_colorizing)
|
| 314 |
+
self.assertTrue(color.allow_colorizing)
|
| 315 |
+
|
| 316 |
+
# Toggle off while colorizing in progress.
|
| 317 |
+
color.toggle_colorize_event()
|
| 318 |
+
self.assertIsNone(color.after_id)
|
| 319 |
+
self.assertTrue(color.colorizing)
|
| 320 |
+
self.assertTrue(color.stop_colorizing)
|
| 321 |
+
self.assertFalse(color.allow_colorizing)
|
| 322 |
+
|
| 323 |
+
# Toggle on while colorizing not in progress.
|
| 324 |
+
color.colorizing = False
|
| 325 |
+
color.toggle_colorize_event()
|
| 326 |
+
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer')
|
| 327 |
+
self.assertFalse(color.colorizing)
|
| 328 |
+
self.assertTrue(color.stop_colorizing)
|
| 329 |
+
self.assertTrue(color.allow_colorizing)
|
| 330 |
+
|
| 331 |
+
@mock.patch.object(colorizer.ColorDelegator, 'recolorize_main')
|
| 332 |
+
def test_recolorize(self, mock_recmain):
|
| 333 |
+
text = self.text
|
| 334 |
+
color = self.color
|
| 335 |
+
eq = self.assertEqual
|
| 336 |
+
# Call recolorize manually and not scheduled.
|
| 337 |
+
text.after_cancel(color.after_id)
|
| 338 |
+
|
| 339 |
+
# No delegate.
|
| 340 |
+
save_delegate = color.delegate
|
| 341 |
+
color.delegate = None
|
| 342 |
+
color.recolorize()
|
| 343 |
+
mock_recmain.assert_not_called()
|
| 344 |
+
color.delegate = save_delegate
|
| 345 |
+
|
| 346 |
+
# Toggle off colorizing.
|
| 347 |
+
color.allow_colorizing = False
|
| 348 |
+
color.recolorize()
|
| 349 |
+
mock_recmain.assert_not_called()
|
| 350 |
+
color.allow_colorizing = True
|
| 351 |
+
|
| 352 |
+
# Colorizing in progress.
|
| 353 |
+
color.colorizing = True
|
| 354 |
+
color.recolorize()
|
| 355 |
+
mock_recmain.assert_not_called()
|
| 356 |
+
color.colorizing = False
|
| 357 |
+
|
| 358 |
+
# Colorizing is done, but not completed, so rescheduled.
|
| 359 |
+
color.recolorize()
|
| 360 |
+
self.assertFalse(color.stop_colorizing)
|
| 361 |
+
self.assertFalse(color.colorizing)
|
| 362 |
+
mock_recmain.assert_called()
|
| 363 |
+
eq(mock_recmain.call_count, 1)
|
| 364 |
+
# Rescheduled when TODO tag still exists.
|
| 365 |
+
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer')
|
| 366 |
+
|
| 367 |
+
# No changes to text, so no scheduling added.
|
| 368 |
+
text.tag_remove('TODO', '1.0', 'end')
|
| 369 |
+
color.recolorize()
|
| 370 |
+
self.assertFalse(color.stop_colorizing)
|
| 371 |
+
self.assertFalse(color.colorizing)
|
| 372 |
+
mock_recmain.assert_called()
|
| 373 |
+
eq(mock_recmain.call_count, 2)
|
| 374 |
+
self.assertIsNone(color.after_id)
|
| 375 |
+
|
| 376 |
+
@mock.patch.object(colorizer.ColorDelegator, 'notify_range')
|
| 377 |
+
def test_recolorize_main(self, mock_notify):
|
| 378 |
+
text = self.text
|
| 379 |
+
color = self.color
|
| 380 |
+
eq = self.assertEqual
|
| 381 |
+
|
| 382 |
+
text.insert('insert', source)
|
| 383 |
+
expected = (('1.0', ('KEYWORD',)), ('1.2', ()), ('1.3', ('KEYWORD',)),
|
| 384 |
+
('1.7', ()), ('1.9', ('BUILTIN',)), ('1.14', ('STRING',)),
|
| 385 |
+
('1.19', ('COMMENT',)),
|
| 386 |
+
('2.1', ('KEYWORD',)), ('2.18', ()), ('2.25', ('COMMENT',)),
|
| 387 |
+
('3.6', ('BUILTIN',)), ('3.12', ('KEYWORD',)), ('3.21', ('COMMENT',)),
|
| 388 |
+
('4.0', ('KEYWORD',)), ('4.3', ()), ('4.6', ()),
|
| 389 |
+
('5.2', ('STRING',)), ('5.8', ('KEYWORD',)), ('5.10', ('STRING',)),
|
| 390 |
+
('6.0', ('KEYWORD',)), ('6.10', ('DEFINITION',)), ('6.11', ()),
|
| 391 |
+
('8.0', ('STRING',)), ('8.4', ()), ('8.5', ('STRING',)),
|
| 392 |
+
('8.12', ()), ('8.14', ('STRING',)),
|
| 393 |
+
('19.0', ('KEYWORD',)),
|
| 394 |
+
('20.4', ('KEYWORD',)), ('20.16', ('KEYWORD',)),# ('20.19', ('KEYWORD',)),
|
| 395 |
+
#('22.4', ('KEYWORD',)), ('22.10', ('KEYWORD',)), ('22.14', ('KEYWORD',)), ('22.19', ('STRING',)),
|
| 396 |
+
#('23.12', ('KEYWORD',)),
|
| 397 |
+
('24.8', ('KEYWORD',)),
|
| 398 |
+
('25.4', ('KEYWORD',)), ('25.9', ('KEYWORD',)),
|
| 399 |
+
('25.11', ('KEYWORD',)), ('25.15', ('STRING',)),
|
| 400 |
+
('25.19', ('KEYWORD',)), ('25.22', ()),
|
| 401 |
+
('25.24', ('KEYWORD',)), ('25.29', ('BUILTIN',)), ('25.37', ('KEYWORD',)),
|
| 402 |
+
('26.4', ('KEYWORD',)), ('26.9', ('KEYWORD',)),# ('26.11', ('KEYWORD',)), ('26.14', (),),
|
| 403 |
+
('27.25', ('STRING',)), ('27.38', ('STRING',)),
|
| 404 |
+
('29.0', ('STRING',)),
|
| 405 |
+
('30.1', ('STRING',)),
|
| 406 |
+
# SYNC at the end of every line.
|
| 407 |
+
('1.55', ('SYNC',)), ('2.50', ('SYNC',)), ('3.34', ('SYNC',)),
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
# Nothing marked to do therefore no tags in text.
|
| 411 |
+
text.tag_remove('TODO', '1.0', 'end')
|
| 412 |
+
color.recolorize_main()
|
| 413 |
+
for tag in text.tag_names():
|
| 414 |
+
with self.subTest(tag=tag):
|
| 415 |
+
eq(text.tag_ranges(tag), ())
|
| 416 |
+
|
| 417 |
+
# Source marked for processing.
|
| 418 |
+
text.tag_add('TODO', '1.0', 'end')
|
| 419 |
+
# Check some indexes.
|
| 420 |
+
color.recolorize_main()
|
| 421 |
+
for index, expected_tags in expected:
|
| 422 |
+
with self.subTest(index=index):
|
| 423 |
+
eq(text.tag_names(index), expected_tags)
|
| 424 |
+
|
| 425 |
+
# Check for some tags for ranges.
|
| 426 |
+
eq(text.tag_nextrange('TODO', '1.0'), ())
|
| 427 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ('1.0', '1.2'))
|
| 428 |
+
eq(text.tag_nextrange('COMMENT', '2.0'), ('2.22', '2.43'))
|
| 429 |
+
eq(text.tag_nextrange('SYNC', '2.0'), ('2.43', '3.0'))
|
| 430 |
+
eq(text.tag_nextrange('STRING', '2.0'), ('4.17', '4.53'))
|
| 431 |
+
eq(text.tag_nextrange('STRING', '8.0'), ('8.0', '8.3'))
|
| 432 |
+
eq(text.tag_nextrange('STRING', '8.3'), ('8.5', '8.12'))
|
| 433 |
+
eq(text.tag_nextrange('STRING', '8.12'), ('8.14', '8.17'))
|
| 434 |
+
eq(text.tag_nextrange('STRING', '8.17'), ('8.19', '8.26'))
|
| 435 |
+
eq(text.tag_nextrange('SYNC', '8.0'), ('8.26', '9.0'))
|
| 436 |
+
eq(text.tag_nextrange('SYNC', '30.0'), ('30.10', '32.0'))
|
| 437 |
+
|
| 438 |
+
def _assert_highlighting(self, source, tag_ranges):
|
| 439 |
+
"""Check highlighting of a given piece of code.
|
| 440 |
+
|
| 441 |
+
This inserts just this code into the Text widget. It will then
|
| 442 |
+
check that the resulting highlighting tag ranges exactly match
|
| 443 |
+
those described in the given `tag_ranges` dict.
|
| 444 |
+
|
| 445 |
+
Note that the irrelevant tags 'sel', 'TODO' and 'SYNC' are
|
| 446 |
+
ignored.
|
| 447 |
+
"""
|
| 448 |
+
text = self.text
|
| 449 |
+
|
| 450 |
+
with mock.patch.object(colorizer.ColorDelegator, 'notify_range'):
|
| 451 |
+
text.delete('1.0', 'end-1c')
|
| 452 |
+
text.insert('insert', source)
|
| 453 |
+
text.tag_add('TODO', '1.0', 'end-1c')
|
| 454 |
+
self.color.recolorize_main()
|
| 455 |
+
|
| 456 |
+
# Make a dict with highlighting tag ranges in the Text widget.
|
| 457 |
+
text_tag_ranges = {}
|
| 458 |
+
for tag in set(text.tag_names()) - {'sel', 'TODO', 'SYNC'}:
|
| 459 |
+
indexes = [rng.string for rng in text.tag_ranges(tag)]
|
| 460 |
+
for index_pair in zip(indexes[::2], indexes[1::2]):
|
| 461 |
+
text_tag_ranges.setdefault(tag, []).append(index_pair)
|
| 462 |
+
|
| 463 |
+
self.assertEqual(text_tag_ranges, tag_ranges)
|
| 464 |
+
|
| 465 |
+
with mock.patch.object(colorizer.ColorDelegator, 'notify_range'):
|
| 466 |
+
text.delete('1.0', 'end-1c')
|
| 467 |
+
|
| 468 |
+
def test_def_statement(self):
|
| 469 |
+
# empty def
|
| 470 |
+
self._assert_highlighting('def', {'KEYWORD': [('1.0', '1.3')]})
|
| 471 |
+
|
| 472 |
+
# def followed by identifier
|
| 473 |
+
self._assert_highlighting('def foo:', {'KEYWORD': [('1.0', '1.3')],
|
| 474 |
+
'DEFINITION': [('1.4', '1.7')]})
|
| 475 |
+
|
| 476 |
+
# def followed by partial identifier
|
| 477 |
+
self._assert_highlighting('def fo', {'KEYWORD': [('1.0', '1.3')],
|
| 478 |
+
'DEFINITION': [('1.4', '1.6')]})
|
| 479 |
+
|
| 480 |
+
# def followed by non-keyword
|
| 481 |
+
self._assert_highlighting('def ++', {'KEYWORD': [('1.0', '1.3')]})
|
| 482 |
+
|
| 483 |
+
def test_match_soft_keyword(self):
|
| 484 |
+
# empty match
|
| 485 |
+
self._assert_highlighting('match', {'KEYWORD': [('1.0', '1.5')]})
|
| 486 |
+
|
| 487 |
+
# match followed by partial identifier
|
| 488 |
+
self._assert_highlighting('match fo', {'KEYWORD': [('1.0', '1.5')]})
|
| 489 |
+
|
| 490 |
+
# match followed by identifier and colon
|
| 491 |
+
self._assert_highlighting('match foo:', {'KEYWORD': [('1.0', '1.5')]})
|
| 492 |
+
|
| 493 |
+
# match followed by keyword
|
| 494 |
+
self._assert_highlighting('match and', {'KEYWORD': [('1.6', '1.9')]})
|
| 495 |
+
|
| 496 |
+
# match followed by builtin with keyword prefix
|
| 497 |
+
self._assert_highlighting('match int:', {'KEYWORD': [('1.0', '1.5')],
|
| 498 |
+
'BUILTIN': [('1.6', '1.9')]})
|
| 499 |
+
|
| 500 |
+
# match followed by non-text operator
|
| 501 |
+
self._assert_highlighting('match^', {})
|
| 502 |
+
self._assert_highlighting('match @', {})
|
| 503 |
+
|
| 504 |
+
# match followed by colon
|
| 505 |
+
self._assert_highlighting('match :', {})
|
| 506 |
+
|
| 507 |
+
# match followed by comma
|
| 508 |
+
self._assert_highlighting('match\t,', {})
|
| 509 |
+
|
| 510 |
+
# match followed by a lone underscore
|
| 511 |
+
self._assert_highlighting('match _:', {'KEYWORD': [('1.0', '1.5')]})
|
| 512 |
+
|
| 513 |
+
def test_case_soft_keyword(self):
|
| 514 |
+
# empty case
|
| 515 |
+
self._assert_highlighting('case', {'KEYWORD': [('1.0', '1.4')]})
|
| 516 |
+
|
| 517 |
+
# case followed by partial identifier
|
| 518 |
+
self._assert_highlighting('case fo', {'KEYWORD': [('1.0', '1.4')]})
|
| 519 |
+
|
| 520 |
+
# case followed by identifier and colon
|
| 521 |
+
self._assert_highlighting('case foo:', {'KEYWORD': [('1.0', '1.4')]})
|
| 522 |
+
|
| 523 |
+
# case followed by keyword
|
| 524 |
+
self._assert_highlighting('case and', {'KEYWORD': [('1.5', '1.8')]})
|
| 525 |
+
|
| 526 |
+
# case followed by builtin with keyword prefix
|
| 527 |
+
self._assert_highlighting('case int:', {'KEYWORD': [('1.0', '1.4')],
|
| 528 |
+
'BUILTIN': [('1.5', '1.8')]})
|
| 529 |
+
|
| 530 |
+
# case followed by non-text operator
|
| 531 |
+
self._assert_highlighting('case^', {})
|
| 532 |
+
self._assert_highlighting('case @', {})
|
| 533 |
+
|
| 534 |
+
# case followed by colon
|
| 535 |
+
self._assert_highlighting('case :', {})
|
| 536 |
+
|
| 537 |
+
# case followed by comma
|
| 538 |
+
self._assert_highlighting('case\t,', {})
|
| 539 |
+
|
| 540 |
+
# case followed by a lone underscore
|
| 541 |
+
self._assert_highlighting('case _:', {'KEYWORD': [('1.0', '1.4'),
|
| 542 |
+
('1.5', '1.6')]})
|
| 543 |
+
|
| 544 |
+
def test_long_multiline_string(self):
|
| 545 |
+
source = textwrap.dedent('''\
|
| 546 |
+
"""a
|
| 547 |
+
b
|
| 548 |
+
c
|
| 549 |
+
d
|
| 550 |
+
e"""
|
| 551 |
+
''')
|
| 552 |
+
self._assert_highlighting(source, {'STRING': [('1.0', '5.4')]})
|
| 553 |
+
|
| 554 |
+
@run_in_tk_mainloop(delay=50)
|
| 555 |
+
def test_incremental_editing(self):
|
| 556 |
+
text = self.text
|
| 557 |
+
eq = self.assertEqual
|
| 558 |
+
|
| 559 |
+
# Simulate typing 'inte'. During this, the highlighting should
|
| 560 |
+
# change from normal to keyword to builtin to normal.
|
| 561 |
+
text.insert('insert', 'i')
|
| 562 |
+
yield
|
| 563 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ())
|
| 564 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ())
|
| 565 |
+
|
| 566 |
+
text.insert('insert', 'n')
|
| 567 |
+
yield
|
| 568 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ())
|
| 569 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ('1.0', '1.2'))
|
| 570 |
+
|
| 571 |
+
text.insert('insert', 't')
|
| 572 |
+
yield
|
| 573 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ('1.0', '1.3'))
|
| 574 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ())
|
| 575 |
+
|
| 576 |
+
text.insert('insert', 'e')
|
| 577 |
+
yield
|
| 578 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ())
|
| 579 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ())
|
| 580 |
+
|
| 581 |
+
# Simulate deleting three characters from the end of 'inte'.
|
| 582 |
+
# During this, the highlighting should change from normal to
|
| 583 |
+
# builtin to keyword to normal.
|
| 584 |
+
text.delete('insert-1c', 'insert')
|
| 585 |
+
yield
|
| 586 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ('1.0', '1.3'))
|
| 587 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ())
|
| 588 |
+
|
| 589 |
+
text.delete('insert-1c', 'insert')
|
| 590 |
+
yield
|
| 591 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ())
|
| 592 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ('1.0', '1.2'))
|
| 593 |
+
|
| 594 |
+
text.delete('insert-1c', 'insert')
|
| 595 |
+
yield
|
| 596 |
+
eq(text.tag_nextrange('BUILTIN', '1.0'), ())
|
| 597 |
+
eq(text.tag_nextrange('KEYWORD', '1.0'), ())
|
| 598 |
+
|
| 599 |
+
@mock.patch.object(colorizer.ColorDelegator, 'recolorize')
|
| 600 |
+
@mock.patch.object(colorizer.ColorDelegator, 'notify_range')
|
| 601 |
+
def test_removecolors(self, mock_notify, mock_recolorize):
|
| 602 |
+
text = self.text
|
| 603 |
+
color = self.color
|
| 604 |
+
text.insert('insert', source)
|
| 605 |
+
|
| 606 |
+
color.recolorize_main()
|
| 607 |
+
# recolorize_main doesn't add these tags.
|
| 608 |
+
text.tag_add("ERROR", "1.0")
|
| 609 |
+
text.tag_add("TODO", "1.0")
|
| 610 |
+
text.tag_add("hit", "1.0")
|
| 611 |
+
for tag in color.tagdefs:
|
| 612 |
+
with self.subTest(tag=tag):
|
| 613 |
+
self.assertNotEqual(text.tag_ranges(tag), ())
|
| 614 |
+
|
| 615 |
+
color.removecolors()
|
| 616 |
+
for tag in color.tagdefs:
|
| 617 |
+
with self.subTest(tag=tag):
|
| 618 |
+
self.assertEqual(text.tag_ranges(tag), ())
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
if __name__ == '__main__':
|
| 622 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_debugger_r.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test debugger_r, coverage 30%."
|
| 2 |
+
|
| 3 |
+
from idlelib import debugger_r
|
| 4 |
+
import unittest
|
| 5 |
+
|
| 6 |
+
# Boilerplate likely to be needed for future test classes.
|
| 7 |
+
##from test.support import requires
|
| 8 |
+
##from tkinter import Tk
|
| 9 |
+
##class Test(unittest.TestCase):
|
| 10 |
+
## @classmethod
|
| 11 |
+
## def setUpClass(cls):
|
| 12 |
+
## requires('gui')
|
| 13 |
+
## cls.root = Tk()
|
| 14 |
+
## @classmethod
|
| 15 |
+
## def tearDownClass(cls):
|
| 16 |
+
## cls.root.destroy()
|
| 17 |
+
|
| 18 |
+
# GUIProxy, IdbAdapter, FrameProxy, CodeProxy, DictProxy,
|
| 19 |
+
# GUIAdapter, IdbProxy, and 7 functions still need tests.
|
| 20 |
+
|
| 21 |
+
class IdbAdapterTest(unittest.TestCase):
|
| 22 |
+
|
| 23 |
+
def test_dict_item_noattr(self): # Issue 33065.
|
| 24 |
+
|
| 25 |
+
class BinData:
|
| 26 |
+
def __repr__(self):
|
| 27 |
+
return self.length
|
| 28 |
+
|
| 29 |
+
debugger_r.dicttable[0] = {'BinData': BinData()}
|
| 30 |
+
idb = debugger_r.IdbAdapter(None)
|
| 31 |
+
self.assertTrue(idb.dict_item(0, 'BinData'))
|
| 32 |
+
debugger_r.dicttable.clear()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
if __name__ == '__main__':
|
| 36 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_editor.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test editor, coverage 35%."
|
| 2 |
+
|
| 3 |
+
from idlelib import editor
|
| 4 |
+
import unittest
|
| 5 |
+
from collections import namedtuple
|
| 6 |
+
from test.support import requires
|
| 7 |
+
from tkinter import Tk
|
| 8 |
+
|
| 9 |
+
Editor = editor.EditorWindow
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class EditorWindowTest(unittest.TestCase):
|
| 13 |
+
|
| 14 |
+
@classmethod
|
| 15 |
+
def setUpClass(cls):
|
| 16 |
+
requires('gui')
|
| 17 |
+
cls.root = Tk()
|
| 18 |
+
cls.root.withdraw()
|
| 19 |
+
|
| 20 |
+
@classmethod
|
| 21 |
+
def tearDownClass(cls):
|
| 22 |
+
cls.root.update_idletasks()
|
| 23 |
+
for id in cls.root.tk.call('after', 'info'):
|
| 24 |
+
cls.root.after_cancel(id)
|
| 25 |
+
cls.root.destroy()
|
| 26 |
+
del cls.root
|
| 27 |
+
|
| 28 |
+
def test_init(self):
|
| 29 |
+
e = Editor(root=self.root)
|
| 30 |
+
self.assertEqual(e.root, self.root)
|
| 31 |
+
e._close()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class TestGetLineIndent(unittest.TestCase):
|
| 35 |
+
def test_empty_lines(self):
|
| 36 |
+
for tabwidth in [1, 2, 4, 6, 8]:
|
| 37 |
+
for line in ['', '\n']:
|
| 38 |
+
with self.subTest(line=line, tabwidth=tabwidth):
|
| 39 |
+
self.assertEqual(
|
| 40 |
+
editor.get_line_indent(line, tabwidth=tabwidth),
|
| 41 |
+
(0, 0),
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
def test_tabwidth_4(self):
|
| 45 |
+
# (line, (raw, effective))
|
| 46 |
+
tests = (('no spaces', (0, 0)),
|
| 47 |
+
# Internal space isn't counted.
|
| 48 |
+
(' space test', (4, 4)),
|
| 49 |
+
('\ttab test', (1, 4)),
|
| 50 |
+
('\t\tdouble tabs test', (2, 8)),
|
| 51 |
+
# Different results when mixing tabs and spaces.
|
| 52 |
+
(' \tmixed test', (5, 8)),
|
| 53 |
+
(' \t mixed test', (5, 6)),
|
| 54 |
+
('\t mixed test', (5, 8)),
|
| 55 |
+
# Spaces not divisible by tabwidth.
|
| 56 |
+
(' \tmixed test', (3, 4)),
|
| 57 |
+
(' \t mixed test', (3, 5)),
|
| 58 |
+
('\t mixed test', (3, 6)),
|
| 59 |
+
# Only checks spaces and tabs.
|
| 60 |
+
('\nnewline test', (0, 0)))
|
| 61 |
+
|
| 62 |
+
for line, expected in tests:
|
| 63 |
+
with self.subTest(line=line):
|
| 64 |
+
self.assertEqual(
|
| 65 |
+
editor.get_line_indent(line, tabwidth=4),
|
| 66 |
+
expected,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
def test_tabwidth_8(self):
|
| 70 |
+
# (line, (raw, effective))
|
| 71 |
+
tests = (('no spaces', (0, 0)),
|
| 72 |
+
# Internal space isn't counted.
|
| 73 |
+
(' space test', (8, 8)),
|
| 74 |
+
('\ttab test', (1, 8)),
|
| 75 |
+
('\t\tdouble tabs test', (2, 16)),
|
| 76 |
+
# Different results when mixing tabs and spaces.
|
| 77 |
+
(' \tmixed test', (9, 16)),
|
| 78 |
+
(' \t mixed test', (9, 10)),
|
| 79 |
+
('\t mixed test', (9, 16)),
|
| 80 |
+
# Spaces not divisible by tabwidth.
|
| 81 |
+
(' \tmixed test', (3, 8)),
|
| 82 |
+
(' \t mixed test', (3, 9)),
|
| 83 |
+
('\t mixed test', (3, 10)),
|
| 84 |
+
# Only checks spaces and tabs.
|
| 85 |
+
('\nnewline test', (0, 0)))
|
| 86 |
+
|
| 87 |
+
for line, expected in tests:
|
| 88 |
+
with self.subTest(line=line):
|
| 89 |
+
self.assertEqual(
|
| 90 |
+
editor.get_line_indent(line, tabwidth=8),
|
| 91 |
+
expected,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def insert(text, string):
|
| 96 |
+
text.delete('1.0', 'end')
|
| 97 |
+
text.insert('end', string)
|
| 98 |
+
text.update() # Force update for colorizer to finish.
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class IndentAndNewlineTest(unittest.TestCase):
|
| 102 |
+
|
| 103 |
+
@classmethod
|
| 104 |
+
def setUpClass(cls):
|
| 105 |
+
requires('gui')
|
| 106 |
+
cls.root = Tk()
|
| 107 |
+
cls.root.withdraw()
|
| 108 |
+
cls.window = Editor(root=cls.root)
|
| 109 |
+
cls.window.indentwidth = 2
|
| 110 |
+
cls.window.tabwidth = 2
|
| 111 |
+
|
| 112 |
+
@classmethod
|
| 113 |
+
def tearDownClass(cls):
|
| 114 |
+
cls.window._close()
|
| 115 |
+
del cls.window
|
| 116 |
+
cls.root.update_idletasks()
|
| 117 |
+
for id in cls.root.tk.call('after', 'info'):
|
| 118 |
+
cls.root.after_cancel(id)
|
| 119 |
+
cls.root.destroy()
|
| 120 |
+
del cls.root
|
| 121 |
+
|
| 122 |
+
def test_indent_and_newline_event(self):
|
| 123 |
+
eq = self.assertEqual
|
| 124 |
+
w = self.window
|
| 125 |
+
text = w.text
|
| 126 |
+
get = text.get
|
| 127 |
+
nl = w.newline_and_indent_event
|
| 128 |
+
|
| 129 |
+
TestInfo = namedtuple('Tests', ['label', 'text', 'expected', 'mark'])
|
| 130 |
+
|
| 131 |
+
tests = (TestInfo('Empty line inserts with no indent.',
|
| 132 |
+
' \n def __init__(self):',
|
| 133 |
+
'\n \n def __init__(self):\n',
|
| 134 |
+
'1.end'),
|
| 135 |
+
TestInfo('Inside bracket before space, deletes space.',
|
| 136 |
+
' def f1(self, a, b):',
|
| 137 |
+
' def f1(self,\n a, b):\n',
|
| 138 |
+
'1.14'),
|
| 139 |
+
TestInfo('Inside bracket after space, deletes space.',
|
| 140 |
+
' def f1(self, a, b):',
|
| 141 |
+
' def f1(self,\n a, b):\n',
|
| 142 |
+
'1.15'),
|
| 143 |
+
TestInfo('Inside string with one line - no indent.',
|
| 144 |
+
' """Docstring."""',
|
| 145 |
+
' """Docstring.\n"""\n',
|
| 146 |
+
'1.15'),
|
| 147 |
+
TestInfo('Inside string with more than one line.',
|
| 148 |
+
' """Docstring.\n Docstring Line 2"""',
|
| 149 |
+
' """Docstring.\n Docstring Line 2\n """\n',
|
| 150 |
+
'2.18'),
|
| 151 |
+
TestInfo('Backslash with one line.',
|
| 152 |
+
'a =\\',
|
| 153 |
+
'a =\\\n \n',
|
| 154 |
+
'1.end'),
|
| 155 |
+
TestInfo('Backslash with more than one line.',
|
| 156 |
+
'a =\\\n multiline\\',
|
| 157 |
+
'a =\\\n multiline\\\n \n',
|
| 158 |
+
'2.end'),
|
| 159 |
+
TestInfo('Block opener - indents +1 level.',
|
| 160 |
+
' def f1(self):\n pass',
|
| 161 |
+
' def f1(self):\n \n pass\n',
|
| 162 |
+
'1.end'),
|
| 163 |
+
TestInfo('Block closer - dedents -1 level.',
|
| 164 |
+
' def f1(self):\n pass',
|
| 165 |
+
' def f1(self):\n pass\n \n',
|
| 166 |
+
'2.end'),
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
for test in tests:
|
| 170 |
+
with self.subTest(label=test.label):
|
| 171 |
+
insert(text, test.text)
|
| 172 |
+
text.mark_set('insert', test.mark)
|
| 173 |
+
nl(event=None)
|
| 174 |
+
eq(get('1.0', 'end'), test.expected)
|
| 175 |
+
|
| 176 |
+
# Selected text.
|
| 177 |
+
insert(text, ' def f1(self, a, b):\n return a + b')
|
| 178 |
+
text.tag_add('sel', '1.17', '1.end')
|
| 179 |
+
nl(None)
|
| 180 |
+
# Deletes selected text before adding new line.
|
| 181 |
+
eq(get('1.0', 'end'), ' def f1(self, a,\n \n return a + b\n')
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
class RMenuTest(unittest.TestCase):
|
| 185 |
+
|
| 186 |
+
@classmethod
|
| 187 |
+
def setUpClass(cls):
|
| 188 |
+
requires('gui')
|
| 189 |
+
cls.root = Tk()
|
| 190 |
+
cls.root.withdraw()
|
| 191 |
+
cls.window = Editor(root=cls.root)
|
| 192 |
+
|
| 193 |
+
@classmethod
|
| 194 |
+
def tearDownClass(cls):
|
| 195 |
+
cls.window._close()
|
| 196 |
+
del cls.window
|
| 197 |
+
cls.root.update_idletasks()
|
| 198 |
+
for id in cls.root.tk.call('after', 'info'):
|
| 199 |
+
cls.root.after_cancel(id)
|
| 200 |
+
cls.root.destroy()
|
| 201 |
+
del cls.root
|
| 202 |
+
|
| 203 |
+
class DummyRMenu:
|
| 204 |
+
def tk_popup(x, y): pass
|
| 205 |
+
|
| 206 |
+
def test_rclick(self):
|
| 207 |
+
pass
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
if __name__ == '__main__':
|
| 211 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_filelist.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test filelist, coverage 19%."
|
| 2 |
+
|
| 3 |
+
from idlelib import filelist
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
from tkinter import Tk
|
| 7 |
+
|
| 8 |
+
class FileListTest(unittest.TestCase):
|
| 9 |
+
|
| 10 |
+
@classmethod
|
| 11 |
+
def setUpClass(cls):
|
| 12 |
+
requires('gui')
|
| 13 |
+
cls.root = Tk()
|
| 14 |
+
cls.root.withdraw()
|
| 15 |
+
|
| 16 |
+
@classmethod
|
| 17 |
+
def tearDownClass(cls):
|
| 18 |
+
cls.root.update_idletasks()
|
| 19 |
+
for id in cls.root.tk.call('after', 'info'):
|
| 20 |
+
cls.root.after_cancel(id)
|
| 21 |
+
cls.root.destroy()
|
| 22 |
+
del cls.root
|
| 23 |
+
|
| 24 |
+
def test_new_empty(self):
|
| 25 |
+
flist = filelist.FileList(self.root)
|
| 26 |
+
self.assertEqual(flist.root, self.root)
|
| 27 |
+
e = flist.new()
|
| 28 |
+
self.assertEqual(type(e), flist.EditorWindow)
|
| 29 |
+
e._close()
|
| 30 |
+
|
| 31 |
+
|
| 32 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|
|
|
|
|
|
| 1 |
+
"Test format, coverage 99%."
|
| 2 |
+
|
| 3 |
+
from idlelib import format as ft
|
| 4 |
+
import unittest
|
| 5 |
+
from unittest import mock
|
| 6 |
+
from test.support import requires
|
| 7 |
+
from tkinter import Tk, Text
|
| 8 |
+
from idlelib.editor import EditorWindow
|
| 9 |
+
from idlelib.idle_test.mock_idle import Editor as MockEditor
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Is_Get_Test(unittest.TestCase):
|
| 13 |
+
"""Test the is_ and get_ functions"""
|
| 14 |
+
test_comment = '# This is a comment'
|
| 15 |
+
test_nocomment = 'This is not a comment'
|
| 16 |
+
trailingws_comment = '# This is a comment '
|
| 17 |
+
leadingws_comment = ' # This is a comment'
|
| 18 |
+
leadingws_nocomment = ' This is not a comment'
|
| 19 |
+
|
| 20 |
+
def test_is_all_white(self):
|
| 21 |
+
self.assertTrue(ft.is_all_white(''))
|
| 22 |
+
self.assertTrue(ft.is_all_white('\t\n\r\f\v'))
|
| 23 |
+
self.assertFalse(ft.is_all_white(self.test_comment))
|
| 24 |
+
|
| 25 |
+
def test_get_indent(self):
|
| 26 |
+
Equal = self.assertEqual
|
| 27 |
+
Equal(ft.get_indent(self.test_comment), '')
|
| 28 |
+
Equal(ft.get_indent(self.trailingws_comment), '')
|
| 29 |
+
Equal(ft.get_indent(self.leadingws_comment), ' ')
|
| 30 |
+
Equal(ft.get_indent(self.leadingws_nocomment), ' ')
|
| 31 |
+
|
| 32 |
+
def test_get_comment_header(self):
|
| 33 |
+
Equal = self.assertEqual
|
| 34 |
+
# Test comment strings
|
| 35 |
+
Equal(ft.get_comment_header(self.test_comment), '#')
|
| 36 |
+
Equal(ft.get_comment_header(self.trailingws_comment), '#')
|
| 37 |
+
Equal(ft.get_comment_header(self.leadingws_comment), ' #')
|
| 38 |
+
# Test non-comment strings
|
| 39 |
+
Equal(ft.get_comment_header(self.leadingws_nocomment), ' ')
|
| 40 |
+
Equal(ft.get_comment_header(self.test_nocomment), '')
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class FindTest(unittest.TestCase):
|
| 44 |
+
"""Test the find_paragraph function in paragraph module.
|
| 45 |
+
|
| 46 |
+
Using the runcase() function, find_paragraph() is called with 'mark' set at
|
| 47 |
+
multiple indexes before and inside the test paragraph.
|
| 48 |
+
|
| 49 |
+
It appears that code with the same indentation as a quoted string is grouped
|
| 50 |
+
as part of the same paragraph, which is probably incorrect behavior.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
@classmethod
|
| 54 |
+
def setUpClass(cls):
|
| 55 |
+
from idlelib.idle_test.mock_tk import Text
|
| 56 |
+
cls.text = Text()
|
| 57 |
+
|
| 58 |
+
def runcase(self, inserttext, stopline, expected):
|
| 59 |
+
# Check that find_paragraph returns the expected paragraph when
|
| 60 |
+
# the mark index is set to beginning, middle, end of each line
|
| 61 |
+
# up to but not including the stop line
|
| 62 |
+
text = self.text
|
| 63 |
+
text.insert('1.0', inserttext)
|
| 64 |
+
for line in range(1, stopline):
|
| 65 |
+
linelength = int(text.index("%d.end" % line).split('.')[1])
|
| 66 |
+
for col in (0, linelength//2, linelength):
|
| 67 |
+
tempindex = "%d.%d" % (line, col)
|
| 68 |
+
self.assertEqual(ft.find_paragraph(text, tempindex), expected)
|
| 69 |
+
text.delete('1.0', 'end')
|
| 70 |
+
|
| 71 |
+
def test_find_comment(self):
|
| 72 |
+
comment = (
|
| 73 |
+
"# Comment block with no blank lines before\n"
|
| 74 |
+
"# Comment line\n"
|
| 75 |
+
"\n")
|
| 76 |
+
self.runcase(comment, 3, ('1.0', '3.0', '#', comment[0:58]))
|
| 77 |
+
|
| 78 |
+
comment = (
|
| 79 |
+
"\n"
|
| 80 |
+
"# Comment block with whitespace line before and after\n"
|
| 81 |
+
"# Comment line\n"
|
| 82 |
+
"\n")
|
| 83 |
+
self.runcase(comment, 4, ('2.0', '4.0', '#', comment[1:70]))
|
| 84 |
+
|
| 85 |
+
comment = (
|
| 86 |
+
"\n"
|
| 87 |
+
" # Indented comment block with whitespace before and after\n"
|
| 88 |
+
" # Comment line\n"
|
| 89 |
+
"\n")
|
| 90 |
+
self.runcase(comment, 4, ('2.0', '4.0', ' #', comment[1:82]))
|
| 91 |
+
|
| 92 |
+
comment = (
|
| 93 |
+
"\n"
|
| 94 |
+
"# Single line comment\n"
|
| 95 |
+
"\n")
|
| 96 |
+
self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:23]))
|
| 97 |
+
|
| 98 |
+
comment = (
|
| 99 |
+
"\n"
|
| 100 |
+
" # Single line comment with leading whitespace\n"
|
| 101 |
+
"\n")
|
| 102 |
+
self.runcase(comment, 3, ('2.0', '3.0', ' #', comment[1:51]))
|
| 103 |
+
|
| 104 |
+
comment = (
|
| 105 |
+
"\n"
|
| 106 |
+
"# Comment immediately followed by code\n"
|
| 107 |
+
"x = 42\n"
|
| 108 |
+
"\n")
|
| 109 |
+
self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:40]))
|
| 110 |
+
|
| 111 |
+
comment = (
|
| 112 |
+
"\n"
|
| 113 |
+
" # Indented comment immediately followed by code\n"
|
| 114 |
+
"x = 42\n"
|
| 115 |
+
"\n")
|
| 116 |
+
self.runcase(comment, 3, ('2.0', '3.0', ' #', comment[1:53]))
|
| 117 |
+
|
| 118 |
+
comment = (
|
| 119 |
+
"\n"
|
| 120 |
+
"# Comment immediately followed by indented code\n"
|
| 121 |
+
" x = 42\n"
|
| 122 |
+
"\n")
|
| 123 |
+
self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:49]))
|
| 124 |
+
|
| 125 |
+
def test_find_paragraph(self):
|
| 126 |
+
teststring = (
|
| 127 |
+
'"""String with no blank lines before\n'
|
| 128 |
+
'String line\n'
|
| 129 |
+
'"""\n'
|
| 130 |
+
'\n')
|
| 131 |
+
self.runcase(teststring, 4, ('1.0', '4.0', '', teststring[0:53]))
|
| 132 |
+
|
| 133 |
+
teststring = (
|
| 134 |
+
"\n"
|
| 135 |
+
'"""String with whitespace line before and after\n'
|
| 136 |
+
'String line.\n'
|
| 137 |
+
'"""\n'
|
| 138 |
+
'\n')
|
| 139 |
+
self.runcase(teststring, 5, ('2.0', '5.0', '', teststring[1:66]))
|
| 140 |
+
|
| 141 |
+
teststring = (
|
| 142 |
+
'\n'
|
| 143 |
+
' """Indented string with whitespace before and after\n'
|
| 144 |
+
' Comment string.\n'
|
| 145 |
+
' """\n'
|
| 146 |
+
'\n')
|
| 147 |
+
self.runcase(teststring, 5, ('2.0', '5.0', ' ', teststring[1:85]))
|
| 148 |
+
|
| 149 |
+
teststring = (
|
| 150 |
+
'\n'
|
| 151 |
+
'"""Single line string."""\n'
|
| 152 |
+
'\n')
|
| 153 |
+
self.runcase(teststring, 3, ('2.0', '3.0', '', teststring[1:27]))
|
| 154 |
+
|
| 155 |
+
teststring = (
|
| 156 |
+
'\n'
|
| 157 |
+
' """Single line string with leading whitespace."""\n'
|
| 158 |
+
'\n')
|
| 159 |
+
self.runcase(teststring, 3, ('2.0', '3.0', ' ', teststring[1:55]))
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class ReformatFunctionTest(unittest.TestCase):
|
| 163 |
+
"""Test the reformat_paragraph function without the editor window."""
|
| 164 |
+
|
| 165 |
+
def test_reformat_paragraph(self):
|
| 166 |
+
Equal = self.assertEqual
|
| 167 |
+
reform = ft.reformat_paragraph
|
| 168 |
+
hw = "O hello world"
|
| 169 |
+
Equal(reform(' ', 1), ' ')
|
| 170 |
+
Equal(reform("Hello world", 20), "Hello world")
|
| 171 |
+
|
| 172 |
+
# Test without leading newline
|
| 173 |
+
Equal(reform(hw, 1), "O\nhello\nworld")
|
| 174 |
+
Equal(reform(hw, 6), "O\nhello\nworld")
|
| 175 |
+
Equal(reform(hw, 7), "O hello\nworld")
|
| 176 |
+
Equal(reform(hw, 12), "O hello\nworld")
|
| 177 |
+
Equal(reform(hw, 13), "O hello world")
|
| 178 |
+
|
| 179 |
+
# Test with leading newline
|
| 180 |
+
hw = "\nO hello world"
|
| 181 |
+
Equal(reform(hw, 1), "\nO\nhello\nworld")
|
| 182 |
+
Equal(reform(hw, 6), "\nO\nhello\nworld")
|
| 183 |
+
Equal(reform(hw, 7), "\nO hello\nworld")
|
| 184 |
+
Equal(reform(hw, 12), "\nO hello\nworld")
|
| 185 |
+
Equal(reform(hw, 13), "\nO hello world")
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
class ReformatCommentTest(unittest.TestCase):
|
| 189 |
+
"""Test the reformat_comment function without the editor window."""
|
| 190 |
+
|
| 191 |
+
def test_reformat_comment(self):
|
| 192 |
+
Equal = self.assertEqual
|
| 193 |
+
|
| 194 |
+
# reformat_comment formats to a minimum of 20 characters
|
| 195 |
+
test_string = (
|
| 196 |
+
" \"\"\"this is a test of a reformat for a triple quoted string"
|
| 197 |
+
" will it reformat to less than 70 characters for me?\"\"\"")
|
| 198 |
+
result = ft.reformat_comment(test_string, 70, " ")
|
| 199 |
+
expected = (
|
| 200 |
+
" \"\"\"this is a test of a reformat for a triple quoted string will it\n"
|
| 201 |
+
" reformat to less than 70 characters for me?\"\"\"")
|
| 202 |
+
Equal(result, expected)
|
| 203 |
+
|
| 204 |
+
test_comment = (
|
| 205 |
+
"# this is a test of a reformat for a triple quoted string will "
|
| 206 |
+
"it reformat to less than 70 characters for me?")
|
| 207 |
+
result = ft.reformat_comment(test_comment, 70, "#")
|
| 208 |
+
expected = (
|
| 209 |
+
"# this is a test of a reformat for a triple quoted string will it\n"
|
| 210 |
+
"# reformat to less than 70 characters for me?")
|
| 211 |
+
Equal(result, expected)
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
class FormatClassTest(unittest.TestCase):
|
| 215 |
+
def test_init_close(self):
|
| 216 |
+
instance = ft.FormatParagraph('editor')
|
| 217 |
+
self.assertEqual(instance.editwin, 'editor')
|
| 218 |
+
instance.close()
|
| 219 |
+
self.assertEqual(instance.editwin, None)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# For testing format_paragraph_event, Initialize FormatParagraph with
|
| 223 |
+
# a mock Editor with .text and .get_selection_indices. The text must
|
| 224 |
+
# be a Text wrapper that adds two methods
|
| 225 |
+
|
| 226 |
+
# A real EditorWindow creates unneeded, time-consuming baggage and
|
| 227 |
+
# sometimes emits shutdown warnings like this:
|
| 228 |
+
# "warning: callback failed in WindowList <class '_tkinter.TclError'>
|
| 229 |
+
# : invalid command name ".55131368.windows".
|
| 230 |
+
# Calling EditorWindow._close in tearDownClass prevents this but causes
|
| 231 |
+
# other problems (windows left open).
|
| 232 |
+
|
| 233 |
+
class TextWrapper:
|
| 234 |
+
def __init__(self, master):
|
| 235 |
+
self.text = Text(master=master)
|
| 236 |
+
def __getattr__(self, name):
|
| 237 |
+
return getattr(self.text, name)
|
| 238 |
+
def undo_block_start(self): pass
|
| 239 |
+
def undo_block_stop(self): pass
|
| 240 |
+
|
| 241 |
+
class Editor:
|
| 242 |
+
def __init__(self, root):
|
| 243 |
+
self.text = TextWrapper(root)
|
| 244 |
+
get_selection_indices = EditorWindow. get_selection_indices
|
| 245 |
+
|
| 246 |
+
class FormatEventTest(unittest.TestCase):
|
| 247 |
+
"""Test the formatting of text inside a Text widget.
|
| 248 |
+
|
| 249 |
+
This is done with FormatParagraph.format.paragraph_event,
|
| 250 |
+
which calls functions in the module as appropriate.
|
| 251 |
+
"""
|
| 252 |
+
test_string = (
|
| 253 |
+
" '''this is a test of a reformat for a triple "
|
| 254 |
+
"quoted string will it reformat to less than 70 "
|
| 255 |
+
"characters for me?'''\n")
|
| 256 |
+
multiline_test_string = (
|
| 257 |
+
" '''The first line is under the max width.\n"
|
| 258 |
+
" The second line's length is way over the max width. It goes "
|
| 259 |
+
"on and on until it is over 100 characters long.\n"
|
| 260 |
+
" Same thing with the third line. It is also way over the max "
|
| 261 |
+
"width, but FormatParagraph will fix it.\n"
|
| 262 |
+
" '''\n")
|
| 263 |
+
multiline_test_comment = (
|
| 264 |
+
"# The first line is under the max width.\n"
|
| 265 |
+
"# The second line's length is way over the max width. It goes on "
|
| 266 |
+
"and on until it is over 100 characters long.\n"
|
| 267 |
+
"# Same thing with the third line. It is also way over the max "
|
| 268 |
+
"width, but FormatParagraph will fix it.\n"
|
| 269 |
+
"# The fourth line is short like the first line.")
|
| 270 |
+
|
| 271 |
+
@classmethod
|
| 272 |
+
def setUpClass(cls):
|
| 273 |
+
requires('gui')
|
| 274 |
+
cls.root = Tk()
|
| 275 |
+
cls.root.withdraw()
|
| 276 |
+
editor = Editor(root=cls.root)
|
| 277 |
+
cls.text = editor.text.text # Test code does not need the wrapper.
|
| 278 |
+
cls.formatter = ft.FormatParagraph(editor).format_paragraph_event
|
| 279 |
+
# Sets the insert mark just after the re-wrapped and inserted text.
|
| 280 |
+
|
| 281 |
+
@classmethod
|
| 282 |
+
def tearDownClass(cls):
|
| 283 |
+
del cls.text, cls.formatter
|
| 284 |
+
cls.root.update_idletasks()
|
| 285 |
+
cls.root.destroy()
|
| 286 |
+
del cls.root
|
| 287 |
+
|
| 288 |
+
def test_short_line(self):
|
| 289 |
+
self.text.insert('1.0', "Short line\n")
|
| 290 |
+
self.formatter("Dummy")
|
| 291 |
+
self.assertEqual(self.text.get('1.0', 'insert'), "Short line\n" )
|
| 292 |
+
self.text.delete('1.0', 'end')
|
| 293 |
+
|
| 294 |
+
def test_long_line(self):
|
| 295 |
+
text = self.text
|
| 296 |
+
|
| 297 |
+
# Set cursor ('insert' mark) to '1.0', within text.
|
| 298 |
+
text.insert('1.0', self.test_string)
|
| 299 |
+
text.mark_set('insert', '1.0')
|
| 300 |
+
self.formatter('ParameterDoesNothing', limit=70)
|
| 301 |
+
result = text.get('1.0', 'insert')
|
| 302 |
+
# find function includes \n
|
| 303 |
+
expected = (
|
| 304 |
+
" '''this is a test of a reformat for a triple quoted string will it\n"
|
| 305 |
+
" reformat to less than 70 characters for me?'''\n") # yes
|
| 306 |
+
self.assertEqual(result, expected)
|
| 307 |
+
text.delete('1.0', 'end')
|
| 308 |
+
|
| 309 |
+
# Select from 1.11 to line end.
|
| 310 |
+
text.insert('1.0', self.test_string)
|
| 311 |
+
text.tag_add('sel', '1.11', '1.end')
|
| 312 |
+
self.formatter('ParameterDoesNothing', limit=70)
|
| 313 |
+
result = text.get('1.0', 'insert')
|
| 314 |
+
# selection excludes \n
|
| 315 |
+
expected = (
|
| 316 |
+
" '''this is a test of a reformat for a triple quoted string will it reformat\n"
|
| 317 |
+
" to less than 70 characters for me?'''") # no
|
| 318 |
+
self.assertEqual(result, expected)
|
| 319 |
+
text.delete('1.0', 'end')
|
| 320 |
+
|
| 321 |
+
def test_multiple_lines(self):
|
| 322 |
+
text = self.text
|
| 323 |
+
# Select 2 long lines.
|
| 324 |
+
text.insert('1.0', self.multiline_test_string)
|
| 325 |
+
text.tag_add('sel', '2.0', '4.0')
|
| 326 |
+
self.formatter('ParameterDoesNothing', limit=70)
|
| 327 |
+
result = text.get('2.0', 'insert')
|
| 328 |
+
expected = (
|
| 329 |
+
" The second line's length is way over the max width. It goes on and\n"
|
| 330 |
+
" on until it is over 100 characters long. Same thing with the third\n"
|
| 331 |
+
" line. It is also way over the max width, but FormatParagraph will\n"
|
| 332 |
+
" fix it.\n")
|
| 333 |
+
self.assertEqual(result, expected)
|
| 334 |
+
text.delete('1.0', 'end')
|
| 335 |
+
|
| 336 |
+
def test_comment_block(self):
|
| 337 |
+
text = self.text
|
| 338 |
+
|
| 339 |
+
# Set cursor ('insert') to '1.0', within block.
|
| 340 |
+
text.insert('1.0', self.multiline_test_comment)
|
| 341 |
+
self.formatter('ParameterDoesNothing', limit=70)
|
| 342 |
+
result = text.get('1.0', 'insert')
|
| 343 |
+
expected = (
|
| 344 |
+
"# The first line is under the max width. The second line's length is\n"
|
| 345 |
+
"# way over the max width. It goes on and on until it is over 100\n"
|
| 346 |
+
"# characters long. Same thing with the third line. It is also way over\n"
|
| 347 |
+
"# the max width, but FormatParagraph will fix it. The fourth line is\n"
|
| 348 |
+
"# short like the first line.\n")
|
| 349 |
+
self.assertEqual(result, expected)
|
| 350 |
+
text.delete('1.0', 'end')
|
| 351 |
+
|
| 352 |
+
# Select line 2, verify line 1 unaffected.
|
| 353 |
+
text.insert('1.0', self.multiline_test_comment)
|
| 354 |
+
text.tag_add('sel', '2.0', '3.0')
|
| 355 |
+
self.formatter('ParameterDoesNothing', limit=70)
|
| 356 |
+
result = text.get('1.0', 'insert')
|
| 357 |
+
expected = (
|
| 358 |
+
"# The first line is under the max width.\n"
|
| 359 |
+
"# The second line's length is way over the max width. It goes on and\n"
|
| 360 |
+
"# on until it is over 100 characters long.\n")
|
| 361 |
+
self.assertEqual(result, expected)
|
| 362 |
+
text.delete('1.0', 'end')
|
| 363 |
+
|
| 364 |
+
# The following block worked with EditorWindow but fails with the mock.
|
| 365 |
+
# Lines 2 and 3 get pasted together even though the previous block left
|
| 366 |
+
# the previous line alone. More investigation is needed.
|
| 367 |
+
## # Select lines 3 and 4
|
| 368 |
+
## text.insert('1.0', self.multiline_test_comment)
|
| 369 |
+
## text.tag_add('sel', '3.0', '5.0')
|
| 370 |
+
## self.formatter('ParameterDoesNothing')
|
| 371 |
+
## result = text.get('3.0', 'insert')
|
| 372 |
+
## expected = (
|
| 373 |
+
##"# Same thing with the third line. It is also way over the max width,\n"
|
| 374 |
+
##"# but FormatParagraph will fix it. The fourth line is short like the\n"
|
| 375 |
+
##"# first line.\n")
|
| 376 |
+
## self.assertEqual(result, expected)
|
| 377 |
+
## text.delete('1.0', 'end')
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
class DummyEditwin:
|
| 381 |
+
def __init__(self, root, text):
|
| 382 |
+
self.root = root
|
| 383 |
+
self.text = text
|
| 384 |
+
self.indentwidth = 4
|
| 385 |
+
self.tabwidth = 4
|
| 386 |
+
self.usetabs = False
|
| 387 |
+
self.context_use_ps1 = True
|
| 388 |
+
|
| 389 |
+
_make_blanks = EditorWindow._make_blanks
|
| 390 |
+
get_selection_indices = EditorWindow.get_selection_indices
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
class FormatRegionTest(unittest.TestCase):
|
| 394 |
+
|
| 395 |
+
@classmethod
|
| 396 |
+
def setUpClass(cls):
|
| 397 |
+
requires('gui')
|
| 398 |
+
cls.root = Tk()
|
| 399 |
+
cls.root.withdraw()
|
| 400 |
+
cls.text = Text(cls.root)
|
| 401 |
+
cls.text.undo_block_start = mock.Mock()
|
| 402 |
+
cls.text.undo_block_stop = mock.Mock()
|
| 403 |
+
cls.editor = DummyEditwin(cls.root, cls.text)
|
| 404 |
+
cls.formatter = ft.FormatRegion(cls.editor)
|
| 405 |
+
|
| 406 |
+
@classmethod
|
| 407 |
+
def tearDownClass(cls):
|
| 408 |
+
del cls.text, cls.formatter, cls.editor
|
| 409 |
+
cls.root.update_idletasks()
|
| 410 |
+
cls.root.destroy()
|
| 411 |
+
del cls.root
|
| 412 |
+
|
| 413 |
+
def setUp(self):
|
| 414 |
+
self.text.insert('1.0', self.code_sample)
|
| 415 |
+
|
| 416 |
+
def tearDown(self):
|
| 417 |
+
self.text.delete('1.0', 'end')
|
| 418 |
+
|
| 419 |
+
code_sample = """\
|
| 420 |
+
# WS line needed for test.
|
| 421 |
+
class C1:
|
| 422 |
+
# Class comment.
|
| 423 |
+
def __init__(self, a, b):
|
| 424 |
+
self.a = a
|
| 425 |
+
self.b = b
|
| 426 |
+
|
| 427 |
+
def compare(self):
|
| 428 |
+
if a > b:
|
| 429 |
+
return a
|
| 430 |
+
elif a < b:
|
| 431 |
+
return b
|
| 432 |
+
else:
|
| 433 |
+
return None
|
| 434 |
+
"""
|
| 435 |
+
|
| 436 |
+
def test_get_region(self):
|
| 437 |
+
get = self.formatter.get_region
|
| 438 |
+
text = self.text
|
| 439 |
+
eq = self.assertEqual
|
| 440 |
+
|
| 441 |
+
# Add selection.
|
| 442 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 443 |
+
expected_lines = ['',
|
| 444 |
+
' def compare(self):',
|
| 445 |
+
' if a > b:',
|
| 446 |
+
'']
|
| 447 |
+
eq(get(), ('7.0', '10.0', '\n'.join(expected_lines), expected_lines))
|
| 448 |
+
|
| 449 |
+
# Remove selection.
|
| 450 |
+
text.tag_remove('sel', '1.0', 'end')
|
| 451 |
+
eq(get(), ('15.0', '16.0', '\n', ['', '']))
|
| 452 |
+
|
| 453 |
+
def test_set_region(self):
|
| 454 |
+
set_ = self.formatter.set_region
|
| 455 |
+
text = self.text
|
| 456 |
+
eq = self.assertEqual
|
| 457 |
+
|
| 458 |
+
save_bell = text.bell
|
| 459 |
+
text.bell = mock.Mock()
|
| 460 |
+
line6 = self.code_sample.splitlines()[5]
|
| 461 |
+
line10 = self.code_sample.splitlines()[9]
|
| 462 |
+
|
| 463 |
+
text.tag_add('sel', '6.0', '11.0')
|
| 464 |
+
head, tail, chars, lines = self.formatter.get_region()
|
| 465 |
+
|
| 466 |
+
# No changes.
|
| 467 |
+
set_(head, tail, chars, lines)
|
| 468 |
+
text.bell.assert_called_once()
|
| 469 |
+
eq(text.get('6.0', '11.0'), chars)
|
| 470 |
+
eq(text.get('sel.first', 'sel.last'), chars)
|
| 471 |
+
text.tag_remove('sel', '1.0', 'end')
|
| 472 |
+
|
| 473 |
+
# Alter selected lines by changing lines and adding a newline.
|
| 474 |
+
newstring = 'added line 1\n\n\n\n'
|
| 475 |
+
newlines = newstring.split('\n')
|
| 476 |
+
set_('7.0', '10.0', chars, newlines)
|
| 477 |
+
# Selection changed.
|
| 478 |
+
eq(text.get('sel.first', 'sel.last'), newstring)
|
| 479 |
+
# Additional line added, so last index is changed.
|
| 480 |
+
eq(text.get('7.0', '11.0'), newstring)
|
| 481 |
+
# Before and after lines unchanged.
|
| 482 |
+
eq(text.get('6.0', '7.0-1c'), line6)
|
| 483 |
+
eq(text.get('11.0', '12.0-1c'), line10)
|
| 484 |
+
text.tag_remove('sel', '1.0', 'end')
|
| 485 |
+
|
| 486 |
+
text.bell = save_bell
|
| 487 |
+
|
| 488 |
+
def test_indent_region_event(self):
|
| 489 |
+
indent = self.formatter.indent_region_event
|
| 490 |
+
text = self.text
|
| 491 |
+
eq = self.assertEqual
|
| 492 |
+
|
| 493 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 494 |
+
indent()
|
| 495 |
+
# Blank lines aren't affected by indent.
|
| 496 |
+
eq(text.get('7.0', '10.0'), ('\n def compare(self):\n if a > b:\n'))
|
| 497 |
+
|
| 498 |
+
def test_dedent_region_event(self):
|
| 499 |
+
dedent = self.formatter.dedent_region_event
|
| 500 |
+
text = self.text
|
| 501 |
+
eq = self.assertEqual
|
| 502 |
+
|
| 503 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 504 |
+
dedent()
|
| 505 |
+
# Blank lines aren't affected by dedent.
|
| 506 |
+
eq(text.get('7.0', '10.0'), ('\ndef compare(self):\n if a > b:\n'))
|
| 507 |
+
|
| 508 |
+
def test_comment_region_event(self):
|
| 509 |
+
comment = self.formatter.comment_region_event
|
| 510 |
+
text = self.text
|
| 511 |
+
eq = self.assertEqual
|
| 512 |
+
|
| 513 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 514 |
+
comment()
|
| 515 |
+
eq(text.get('7.0', '10.0'), ('##\n## def compare(self):\n## if a > b:\n'))
|
| 516 |
+
|
| 517 |
+
def test_uncomment_region_event(self):
|
| 518 |
+
comment = self.formatter.comment_region_event
|
| 519 |
+
uncomment = self.formatter.uncomment_region_event
|
| 520 |
+
text = self.text
|
| 521 |
+
eq = self.assertEqual
|
| 522 |
+
|
| 523 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 524 |
+
comment()
|
| 525 |
+
uncomment()
|
| 526 |
+
eq(text.get('7.0', '10.0'), ('\n def compare(self):\n if a > b:\n'))
|
| 527 |
+
|
| 528 |
+
# Only remove comments at the beginning of a line.
|
| 529 |
+
text.tag_remove('sel', '1.0', 'end')
|
| 530 |
+
text.tag_add('sel', '3.0', '4.0')
|
| 531 |
+
uncomment()
|
| 532 |
+
eq(text.get('3.0', '3.end'), (' # Class comment.'))
|
| 533 |
+
|
| 534 |
+
self.formatter.set_region('3.0', '4.0', '', ['# Class comment.', ''])
|
| 535 |
+
uncomment()
|
| 536 |
+
eq(text.get('3.0', '3.end'), (' Class comment.'))
|
| 537 |
+
|
| 538 |
+
@mock.patch.object(ft.FormatRegion, "_asktabwidth")
|
| 539 |
+
def test_tabify_region_event(self, _asktabwidth):
|
| 540 |
+
tabify = self.formatter.tabify_region_event
|
| 541 |
+
text = self.text
|
| 542 |
+
eq = self.assertEqual
|
| 543 |
+
|
| 544 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 545 |
+
# No tabwidth selected.
|
| 546 |
+
_asktabwidth.return_value = None
|
| 547 |
+
self.assertIsNone(tabify())
|
| 548 |
+
|
| 549 |
+
_asktabwidth.return_value = 3
|
| 550 |
+
self.assertIsNotNone(tabify())
|
| 551 |
+
eq(text.get('7.0', '10.0'), ('\n\t def compare(self):\n\t\t if a > b:\n'))
|
| 552 |
+
|
| 553 |
+
@mock.patch.object(ft.FormatRegion, "_asktabwidth")
|
| 554 |
+
def test_untabify_region_event(self, _asktabwidth):
|
| 555 |
+
untabify = self.formatter.untabify_region_event
|
| 556 |
+
text = self.text
|
| 557 |
+
eq = self.assertEqual
|
| 558 |
+
|
| 559 |
+
text.tag_add('sel', '7.0', '10.0')
|
| 560 |
+
# No tabwidth selected.
|
| 561 |
+
_asktabwidth.return_value = None
|
| 562 |
+
self.assertIsNone(untabify())
|
| 563 |
+
|
| 564 |
+
_asktabwidth.return_value = 2
|
| 565 |
+
self.formatter.tabify_region_event()
|
| 566 |
+
_asktabwidth.return_value = 3
|
| 567 |
+
self.assertIsNotNone(untabify())
|
| 568 |
+
eq(text.get('7.0', '10.0'), ('\n def compare(self):\n if a > b:\n'))
|
| 569 |
+
|
| 570 |
+
@mock.patch.object(ft, "askinteger")
|
| 571 |
+
def test_ask_tabwidth(self, askinteger):
|
| 572 |
+
ask = self.formatter._asktabwidth
|
| 573 |
+
askinteger.return_value = 10
|
| 574 |
+
self.assertEqual(ask(), 10)
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
class IndentsTest(unittest.TestCase):
|
| 578 |
+
|
| 579 |
+
@mock.patch.object(ft, "askyesno")
|
| 580 |
+
def test_toggle_tabs(self, askyesno):
|
| 581 |
+
editor = DummyEditwin(None, None) # usetabs == False.
|
| 582 |
+
indents = ft.Indents(editor)
|
| 583 |
+
askyesno.return_value = True
|
| 584 |
+
|
| 585 |
+
indents.toggle_tabs_event(None)
|
| 586 |
+
self.assertEqual(editor.usetabs, True)
|
| 587 |
+
self.assertEqual(editor.indentwidth, 8)
|
| 588 |
+
|
| 589 |
+
indents.toggle_tabs_event(None)
|
| 590 |
+
self.assertEqual(editor.usetabs, False)
|
| 591 |
+
self.assertEqual(editor.indentwidth, 8)
|
| 592 |
+
|
| 593 |
+
@mock.patch.object(ft, "askinteger")
|
| 594 |
+
def test_change_indentwidth(self, askinteger):
|
| 595 |
+
editor = DummyEditwin(None, None) # indentwidth == 4.
|
| 596 |
+
indents = ft.Indents(editor)
|
| 597 |
+
|
| 598 |
+
askinteger.return_value = None
|
| 599 |
+
indents.change_indentwidth_event(None)
|
| 600 |
+
self.assertEqual(editor.indentwidth, 4)
|
| 601 |
+
|
| 602 |
+
askinteger.return_value = 3
|
| 603 |
+
indents.change_indentwidth_event(None)
|
| 604 |
+
self.assertEqual(editor.indentwidth, 3)
|
| 605 |
+
|
| 606 |
+
askinteger.return_value = 5
|
| 607 |
+
editor.usetabs = True
|
| 608 |
+
indents.change_indentwidth_event(None)
|
| 609 |
+
self.assertEqual(editor.indentwidth, 3)
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
class RstripTest(unittest.TestCase):
|
| 613 |
+
|
| 614 |
+
@classmethod
|
| 615 |
+
def setUpClass(cls):
|
| 616 |
+
requires('gui')
|
| 617 |
+
cls.root = Tk()
|
| 618 |
+
cls.root.withdraw()
|
| 619 |
+
cls.text = Text(cls.root)
|
| 620 |
+
cls.editor = MockEditor(text=cls.text)
|
| 621 |
+
cls.do_rstrip = ft.Rstrip(cls.editor).do_rstrip
|
| 622 |
+
|
| 623 |
+
@classmethod
|
| 624 |
+
def tearDownClass(cls):
|
| 625 |
+
del cls.text, cls.do_rstrip, cls.editor
|
| 626 |
+
cls.root.update_idletasks()
|
| 627 |
+
cls.root.destroy()
|
| 628 |
+
del cls.root
|
| 629 |
+
|
| 630 |
+
def tearDown(self):
|
| 631 |
+
self.text.delete('1.0', 'end-1c')
|
| 632 |
+
|
| 633 |
+
def test_rstrip_lines(self):
|
| 634 |
+
original = (
|
| 635 |
+
"Line with an ending tab \n"
|
| 636 |
+
"Line ending in 5 spaces \n"
|
| 637 |
+
"Linewithnospaces\n"
|
| 638 |
+
" indented line\n"
|
| 639 |
+
" indented line with trailing space \n"
|
| 640 |
+
" \n")
|
| 641 |
+
stripped = (
|
| 642 |
+
"Line with an ending tab\n"
|
| 643 |
+
"Line ending in 5 spaces\n"
|
| 644 |
+
"Linewithnospaces\n"
|
| 645 |
+
" indented line\n"
|
| 646 |
+
" indented line with trailing space\n")
|
| 647 |
+
|
| 648 |
+
self.text.insert('1.0', original)
|
| 649 |
+
self.do_rstrip()
|
| 650 |
+
self.assertEqual(self.text.get('1.0', 'insert'), stripped)
|
| 651 |
+
|
| 652 |
+
def test_rstrip_end(self):
|
| 653 |
+
text = self.text
|
| 654 |
+
for code in ('', '\n', '\n\n\n'):
|
| 655 |
+
with self.subTest(code=code):
|
| 656 |
+
text.insert('1.0', code)
|
| 657 |
+
self.do_rstrip()
|
| 658 |
+
self.assertEqual(text.get('1.0','end-1c'), '')
|
| 659 |
+
for code in ('a\n', 'a\n\n', 'a\n\n\n'):
|
| 660 |
+
with self.subTest(code=code):
|
| 661 |
+
text.delete('1.0', 'end-1c')
|
| 662 |
+
text.insert('1.0', code)
|
| 663 |
+
self.do_rstrip()
|
| 664 |
+
self.assertEqual(text.get('1.0','end-1c'), 'a\n')
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
if __name__ == '__main__':
|
| 668 |
+
unittest.main(verbosity=2, exit=2)
|
llava/lib/python3.10/idlel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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test hyperparser, coverage 98%."
|
| 2 |
+
|
| 3 |
+
from idlelib.hyperparser import HyperParser
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
from tkinter import Tk, Text
|
| 7 |
+
from idlelib.editor import EditorWindow
|
| 8 |
+
|
| 9 |
+
class DummyEditwin:
|
| 10 |
+
def __init__(self, text):
|
| 11 |
+
self.text = text
|
| 12 |
+
self.indentwidth = 8
|
| 13 |
+
self.tabwidth = 8
|
| 14 |
+
self.prompt_last_line = '>>>'
|
| 15 |
+
self.num_context_lines = 50, 500, 1000
|
| 16 |
+
|
| 17 |
+
_build_char_in_string_func = EditorWindow._build_char_in_string_func
|
| 18 |
+
is_char_in_string = EditorWindow.is_char_in_string
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class HyperParserTest(unittest.TestCase):
|
| 22 |
+
code = (
|
| 23 |
+
'"""This is a module docstring"""\n'
|
| 24 |
+
'# this line is a comment\n'
|
| 25 |
+
'x = "this is a string"\n'
|
| 26 |
+
"y = 'this is also a string'\n"
|
| 27 |
+
'l = [i for i in range(10)]\n'
|
| 28 |
+
'm = [py*py for # comment\n'
|
| 29 |
+
' py in l]\n'
|
| 30 |
+
'x.__len__\n'
|
| 31 |
+
"z = ((r'asdf')+('a')))\n"
|
| 32 |
+
'[x for x in\n'
|
| 33 |
+
'for = False\n'
|
| 34 |
+
'cliché = "this is a string with unicode, what a cliché"'
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
@classmethod
|
| 38 |
+
def setUpClass(cls):
|
| 39 |
+
requires('gui')
|
| 40 |
+
cls.root = Tk()
|
| 41 |
+
cls.root.withdraw()
|
| 42 |
+
cls.text = Text(cls.root)
|
| 43 |
+
cls.editwin = DummyEditwin(cls.text)
|
| 44 |
+
|
| 45 |
+
@classmethod
|
| 46 |
+
def tearDownClass(cls):
|
| 47 |
+
del cls.text, cls.editwin
|
| 48 |
+
cls.root.destroy()
|
| 49 |
+
del cls.root
|
| 50 |
+
|
| 51 |
+
def setUp(self):
|
| 52 |
+
self.text.insert('insert', self.code)
|
| 53 |
+
|
| 54 |
+
def tearDown(self):
|
| 55 |
+
self.text.delete('1.0', 'end')
|
| 56 |
+
self.editwin.prompt_last_line = '>>>'
|
| 57 |
+
|
| 58 |
+
def get_parser(self, index):
|
| 59 |
+
"""
|
| 60 |
+
Return a parser object with index at 'index'
|
| 61 |
+
"""
|
| 62 |
+
return HyperParser(self.editwin, index)
|
| 63 |
+
|
| 64 |
+
def test_init(self):
|
| 65 |
+
"""
|
| 66 |
+
test corner cases in the init method
|
| 67 |
+
"""
|
| 68 |
+
with self.assertRaises(ValueError) as ve:
|
| 69 |
+
self.text.tag_add('console', '1.0', '1.end')
|
| 70 |
+
p = self.get_parser('1.5')
|
| 71 |
+
self.assertIn('precedes', str(ve.exception))
|
| 72 |
+
|
| 73 |
+
# test without ps1
|
| 74 |
+
self.editwin.prompt_last_line = ''
|
| 75 |
+
|
| 76 |
+
# number of lines lesser than 50
|
| 77 |
+
p = self.get_parser('end')
|
| 78 |
+
self.assertEqual(p.rawtext, self.text.get('1.0', 'end'))
|
| 79 |
+
|
| 80 |
+
# number of lines greater than 50
|
| 81 |
+
self.text.insert('end', self.text.get('1.0', 'end')*4)
|
| 82 |
+
p = self.get_parser('54.5')
|
| 83 |
+
|
| 84 |
+
def test_is_in_string(self):
|
| 85 |
+
get = self.get_parser
|
| 86 |
+
|
| 87 |
+
p = get('1.0')
|
| 88 |
+
self.assertFalse(p.is_in_string())
|
| 89 |
+
p = get('1.4')
|
| 90 |
+
self.assertTrue(p.is_in_string())
|
| 91 |
+
p = get('2.3')
|
| 92 |
+
self.assertFalse(p.is_in_string())
|
| 93 |
+
p = get('3.3')
|
| 94 |
+
self.assertFalse(p.is_in_string())
|
| 95 |
+
p = get('3.7')
|
| 96 |
+
self.assertTrue(p.is_in_string())
|
| 97 |
+
p = get('4.6')
|
| 98 |
+
self.assertTrue(p.is_in_string())
|
| 99 |
+
p = get('12.54')
|
| 100 |
+
self.assertTrue(p.is_in_string())
|
| 101 |
+
|
| 102 |
+
def test_is_in_code(self):
|
| 103 |
+
get = self.get_parser
|
| 104 |
+
|
| 105 |
+
p = get('1.0')
|
| 106 |
+
self.assertTrue(p.is_in_code())
|
| 107 |
+
p = get('1.1')
|
| 108 |
+
self.assertFalse(p.is_in_code())
|
| 109 |
+
p = get('2.5')
|
| 110 |
+
self.assertFalse(p.is_in_code())
|
| 111 |
+
p = get('3.4')
|
| 112 |
+
self.assertTrue(p.is_in_code())
|
| 113 |
+
p = get('3.6')
|
| 114 |
+
self.assertFalse(p.is_in_code())
|
| 115 |
+
p = get('4.14')
|
| 116 |
+
self.assertFalse(p.is_in_code())
|
| 117 |
+
|
| 118 |
+
def test_get_surrounding_bracket(self):
|
| 119 |
+
get = self.get_parser
|
| 120 |
+
|
| 121 |
+
def without_mustclose(parser):
|
| 122 |
+
# a utility function to get surrounding bracket
|
| 123 |
+
# with mustclose=False
|
| 124 |
+
return parser.get_surrounding_brackets(mustclose=False)
|
| 125 |
+
|
| 126 |
+
def with_mustclose(parser):
|
| 127 |
+
# a utility function to get surrounding bracket
|
| 128 |
+
# with mustclose=True
|
| 129 |
+
return parser.get_surrounding_brackets(mustclose=True)
|
| 130 |
+
|
| 131 |
+
p = get('3.2')
|
| 132 |
+
self.assertIsNone(with_mustclose(p))
|
| 133 |
+
self.assertIsNone(without_mustclose(p))
|
| 134 |
+
|
| 135 |
+
p = get('5.6')
|
| 136 |
+
self.assertTupleEqual(without_mustclose(p), ('5.4', '5.25'))
|
| 137 |
+
self.assertTupleEqual(without_mustclose(p), with_mustclose(p))
|
| 138 |
+
|
| 139 |
+
p = get('5.23')
|
| 140 |
+
self.assertTupleEqual(without_mustclose(p), ('5.21', '5.24'))
|
| 141 |
+
self.assertTupleEqual(without_mustclose(p), with_mustclose(p))
|
| 142 |
+
|
| 143 |
+
p = get('6.15')
|
| 144 |
+
self.assertTupleEqual(without_mustclose(p), ('6.4', '6.end'))
|
| 145 |
+
self.assertIsNone(with_mustclose(p))
|
| 146 |
+
|
| 147 |
+
p = get('9.end')
|
| 148 |
+
self.assertIsNone(with_mustclose(p))
|
| 149 |
+
self.assertIsNone(without_mustclose(p))
|
| 150 |
+
|
| 151 |
+
def test_get_expression(self):
|
| 152 |
+
get = self.get_parser
|
| 153 |
+
|
| 154 |
+
p = get('4.2')
|
| 155 |
+
self.assertEqual(p.get_expression(), 'y ')
|
| 156 |
+
|
| 157 |
+
p = get('4.7')
|
| 158 |
+
with self.assertRaises(ValueError) as ve:
|
| 159 |
+
p.get_expression()
|
| 160 |
+
self.assertIn('is inside a code', str(ve.exception))
|
| 161 |
+
|
| 162 |
+
p = get('5.25')
|
| 163 |
+
self.assertEqual(p.get_expression(), 'range(10)')
|
| 164 |
+
|
| 165 |
+
p = get('6.7')
|
| 166 |
+
self.assertEqual(p.get_expression(), 'py')
|
| 167 |
+
|
| 168 |
+
p = get('6.8')
|
| 169 |
+
self.assertEqual(p.get_expression(), '')
|
| 170 |
+
|
| 171 |
+
p = get('7.9')
|
| 172 |
+
self.assertEqual(p.get_expression(), 'py')
|
| 173 |
+
|
| 174 |
+
p = get('8.end')
|
| 175 |
+
self.assertEqual(p.get_expression(), 'x.__len__')
|
| 176 |
+
|
| 177 |
+
p = get('9.13')
|
| 178 |
+
self.assertEqual(p.get_expression(), "r'asdf'")
|
| 179 |
+
|
| 180 |
+
p = get('9.17')
|
| 181 |
+
with self.assertRaises(ValueError) as ve:
|
| 182 |
+
p.get_expression()
|
| 183 |
+
self.assertIn('is inside a code', str(ve.exception))
|
| 184 |
+
|
| 185 |
+
p = get('10.0')
|
| 186 |
+
self.assertEqual(p.get_expression(), '')
|
| 187 |
+
|
| 188 |
+
p = get('10.6')
|
| 189 |
+
self.assertEqual(p.get_expression(), '')
|
| 190 |
+
|
| 191 |
+
p = get('10.11')
|
| 192 |
+
self.assertEqual(p.get_expression(), '')
|
| 193 |
+
|
| 194 |
+
p = get('11.3')
|
| 195 |
+
self.assertEqual(p.get_expression(), '')
|
| 196 |
+
|
| 197 |
+
p = get('11.11')
|
| 198 |
+
self.assertEqual(p.get_expression(), 'False')
|
| 199 |
+
|
| 200 |
+
p = get('12.6')
|
| 201 |
+
self.assertEqual(p.get_expression(), 'cliché')
|
| 202 |
+
|
| 203 |
+
def test_eat_identifier(self):
|
| 204 |
+
def is_valid_id(candidate):
|
| 205 |
+
result = HyperParser._eat_identifier(candidate, 0, len(candidate))
|
| 206 |
+
if result == len(candidate):
|
| 207 |
+
return True
|
| 208 |
+
elif result == 0:
|
| 209 |
+
return False
|
| 210 |
+
else:
|
| 211 |
+
err_msg = "Unexpected result: {} (expected 0 or {}".format(
|
| 212 |
+
result, len(candidate)
|
| 213 |
+
)
|
| 214 |
+
raise Exception(err_msg)
|
| 215 |
+
|
| 216 |
+
# invalid first character which is valid elsewhere in an identifier
|
| 217 |
+
self.assertFalse(is_valid_id('2notid'))
|
| 218 |
+
|
| 219 |
+
# ASCII-only valid identifiers
|
| 220 |
+
self.assertTrue(is_valid_id('valid_id'))
|
| 221 |
+
self.assertTrue(is_valid_id('_valid_id'))
|
| 222 |
+
self.assertTrue(is_valid_id('valid_id_'))
|
| 223 |
+
self.assertTrue(is_valid_id('_2valid_id'))
|
| 224 |
+
|
| 225 |
+
# keywords which should be "eaten"
|
| 226 |
+
self.assertTrue(is_valid_id('True'))
|
| 227 |
+
self.assertTrue(is_valid_id('False'))
|
| 228 |
+
self.assertTrue(is_valid_id('None'))
|
| 229 |
+
|
| 230 |
+
# keywords which should not be "eaten"
|
| 231 |
+
self.assertFalse(is_valid_id('for'))
|
| 232 |
+
self.assertFalse(is_valid_id('import'))
|
| 233 |
+
self.assertFalse(is_valid_id('return'))
|
| 234 |
+
|
| 235 |
+
# valid unicode identifiers
|
| 236 |
+
self.assertTrue(is_valid_id('cliche'))
|
| 237 |
+
self.assertTrue(is_valid_id('cliché'))
|
| 238 |
+
self.assertTrue(is_valid_id('a٢'))
|
| 239 |
+
|
| 240 |
+
# invalid unicode identifiers
|
| 241 |
+
self.assertFalse(is_valid_id('2a'))
|
| 242 |
+
self.assertFalse(is_valid_id('٢a'))
|
| 243 |
+
self.assertFalse(is_valid_id('a²'))
|
| 244 |
+
|
| 245 |
+
# valid identifier after "punctuation"
|
| 246 |
+
self.assertEqual(HyperParser._eat_identifier('+ var', 0, 5), len('var'))
|
| 247 |
+
self.assertEqual(HyperParser._eat_identifier('+var', 0, 4), len('var'))
|
| 248 |
+
self.assertEqual(HyperParser._eat_identifier('.var', 0, 4), len('var'))
|
| 249 |
+
|
| 250 |
+
# invalid identifiers
|
| 251 |
+
self.assertFalse(is_valid_id('+'))
|
| 252 |
+
self.assertFalse(is_valid_id(' '))
|
| 253 |
+
self.assertFalse(is_valid_id(':'))
|
| 254 |
+
self.assertFalse(is_valid_id('?'))
|
| 255 |
+
self.assertFalse(is_valid_id('^'))
|
| 256 |
+
self.assertFalse(is_valid_id('\\'))
|
| 257 |
+
self.assertFalse(is_valid_id('"'))
|
| 258 |
+
self.assertFalse(is_valid_id('"a string"'))
|
| 259 |
+
|
| 260 |
+
def test_eat_identifier_various_lengths(self):
|
| 261 |
+
eat_id = HyperParser._eat_identifier
|
| 262 |
+
|
| 263 |
+
for length in range(1, 21):
|
| 264 |
+
self.assertEqual(eat_id('a' * length, 0, length), length)
|
| 265 |
+
self.assertEqual(eat_id('é' * length, 0, length), length)
|
| 266 |
+
self.assertEqual(eat_id('a' + '2' * (length - 1), 0, length), length)
|
| 267 |
+
self.assertEqual(eat_id('é' + '2' * (length - 1), 0, length), length)
|
| 268 |
+
self.assertEqual(eat_id('é' + 'a' * (length - 1), 0, length), length)
|
| 269 |
+
self.assertEqual(eat_id('é' * (length - 1) + 'a', 0, length), length)
|
| 270 |
+
self.assertEqual(eat_id('+' * length, 0, length), 0)
|
| 271 |
+
self.assertEqual(eat_id('2' + 'a' * (length - 1), 0, length), 0)
|
| 272 |
+
self.assertEqual(eat_id('2' + 'é' * (length - 1), 0, length), 0)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
if __name__ == '__main__':
|
| 276 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_iomenu.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test , coverage 17%."
|
| 2 |
+
|
| 3 |
+
from idlelib import iomenu
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
from tkinter import Tk
|
| 7 |
+
from idlelib.editor import EditorWindow
|
| 8 |
+
from idlelib import util
|
| 9 |
+
from idlelib.idle_test.mock_idle import Func
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class IOBindingTest(unittest.TestCase):
|
| 13 |
+
|
| 14 |
+
@classmethod
|
| 15 |
+
def setUpClass(cls):
|
| 16 |
+
requires('gui')
|
| 17 |
+
cls.root = Tk()
|
| 18 |
+
cls.root.withdraw()
|
| 19 |
+
cls.editwin = EditorWindow(root=cls.root)
|
| 20 |
+
cls.io = iomenu.IOBinding(cls.editwin)
|
| 21 |
+
|
| 22 |
+
@classmethod
|
| 23 |
+
def tearDownClass(cls):
|
| 24 |
+
cls.io.close()
|
| 25 |
+
cls.editwin._close()
|
| 26 |
+
del cls.editwin
|
| 27 |
+
cls.root.update_idletasks()
|
| 28 |
+
for id in cls.root.tk.call('after', 'info'):
|
| 29 |
+
cls.root.after_cancel(id) # Need for EditorWindow.
|
| 30 |
+
cls.root.destroy()
|
| 31 |
+
del cls.root
|
| 32 |
+
|
| 33 |
+
def test_init(self):
|
| 34 |
+
self.assertIs(self.io.editwin, self.editwin)
|
| 35 |
+
|
| 36 |
+
def test_fixnewlines_end(self):
|
| 37 |
+
eq = self.assertEqual
|
| 38 |
+
io = self.io
|
| 39 |
+
fix = io.fixnewlines
|
| 40 |
+
text = io.editwin.text
|
| 41 |
+
|
| 42 |
+
# Make the editor temporarily look like Shell.
|
| 43 |
+
self.editwin.interp = None
|
| 44 |
+
shelltext = '>>> if 1'
|
| 45 |
+
self.editwin.get_prompt_text = Func(result=shelltext)
|
| 46 |
+
eq(fix(), shelltext) # Get... call and '\n' not added.
|
| 47 |
+
del self.editwin.interp, self.editwin.get_prompt_text
|
| 48 |
+
|
| 49 |
+
text.insert(1.0, 'a')
|
| 50 |
+
eq(fix(), 'a'+io.eol_convention)
|
| 51 |
+
eq(text.get('1.0', 'end-1c'), 'a\n')
|
| 52 |
+
eq(fix(), 'a'+io.eol_convention)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _extension_in_filetypes(extension):
|
| 56 |
+
return any(
|
| 57 |
+
f'*{extension}' in filetype_tuple[1]
|
| 58 |
+
for filetype_tuple in iomenu.IOBinding.filetypes
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class FiletypesTest(unittest.TestCase):
|
| 63 |
+
def test_python_source_files(self):
|
| 64 |
+
for extension in util.py_extensions:
|
| 65 |
+
with self.subTest(extension=extension):
|
| 66 |
+
self.assertTrue(
|
| 67 |
+
_extension_in_filetypes(extension)
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
def test_text_files(self):
|
| 71 |
+
self.assertTrue(_extension_in_filetypes('.txt'))
|
| 72 |
+
|
| 73 |
+
def test_all_files(self):
|
| 74 |
+
self.assertTrue(_extension_in_filetypes(''))
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_pyshell.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test pyshell, coverage 12%."
|
| 2 |
+
# Plus coverage of test_warning. Was 20% with test_openshell.
|
| 3 |
+
|
| 4 |
+
from idlelib import pyshell
|
| 5 |
+
import unittest
|
| 6 |
+
from test.support import requires
|
| 7 |
+
from tkinter import Tk
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class FunctionTest(unittest.TestCase):
|
| 11 |
+
# Test stand-alone module level non-gui functions.
|
| 12 |
+
|
| 13 |
+
def test_restart_line_wide(self):
|
| 14 |
+
eq = self.assertEqual
|
| 15 |
+
for file, mul, extra in (('', 22, ''), ('finame', 21, '=')):
|
| 16 |
+
width = 60
|
| 17 |
+
bar = mul * '='
|
| 18 |
+
with self.subTest(file=file, bar=bar):
|
| 19 |
+
file = file or 'Shell'
|
| 20 |
+
line = pyshell.restart_line(width, file)
|
| 21 |
+
eq(len(line), width)
|
| 22 |
+
eq(line, f"{bar+extra} RESTART: {file} {bar}")
|
| 23 |
+
|
| 24 |
+
def test_restart_line_narrow(self):
|
| 25 |
+
expect, taglen = "= RESTART: Shell", 16
|
| 26 |
+
for width in (taglen-1, taglen, taglen+1):
|
| 27 |
+
with self.subTest(width=width):
|
| 28 |
+
self.assertEqual(pyshell.restart_line(width, ''), expect)
|
| 29 |
+
self.assertEqual(pyshell.restart_line(taglen+2, ''), expect+' =')
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class PyShellFileListTest(unittest.TestCase):
|
| 33 |
+
|
| 34 |
+
@classmethod
|
| 35 |
+
def setUpClass(cls):
|
| 36 |
+
requires('gui')
|
| 37 |
+
cls.root = Tk()
|
| 38 |
+
cls.root.withdraw()
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def tearDownClass(cls):
|
| 42 |
+
#cls.root.update_idletasks()
|
| 43 |
+
## for id in cls.root.tk.call('after', 'info'):
|
| 44 |
+
## cls.root.after_cancel(id) # Need for EditorWindow.
|
| 45 |
+
cls.root.destroy()
|
| 46 |
+
del cls.root
|
| 47 |
+
|
| 48 |
+
def test_init(self):
|
| 49 |
+
psfl = pyshell.PyShellFileList(self.root)
|
| 50 |
+
self.assertEqual(psfl.EditorWindow, pyshell.PyShellEditorWindow)
|
| 51 |
+
self.assertIsNone(psfl.pyshell)
|
| 52 |
+
|
| 53 |
+
# The following sometimes causes 'invalid command name "109734456recolorize"'.
|
| 54 |
+
# Uncommenting after_cancel above prevents this, but results in
|
| 55 |
+
# TclError: bad window path name ".!listedtoplevel.!frame.text"
|
| 56 |
+
# which is normally prevented by after_cancel.
|
| 57 |
+
## def test_openshell(self):
|
| 58 |
+
## pyshell.use_subprocess = False
|
| 59 |
+
## ps = pyshell.PyShellFileList(self.root).open_shell()
|
| 60 |
+
## self.assertIsInstance(ps, pyshell.PyShell)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class PyShellRemoveLastNewlineAndSurroundingWhitespaceTest(unittest.TestCase):
|
| 64 |
+
regexp = pyshell.PyShell._last_newline_re
|
| 65 |
+
|
| 66 |
+
def all_removed(self, text):
|
| 67 |
+
self.assertEqual('', self.regexp.sub('', text))
|
| 68 |
+
|
| 69 |
+
def none_removed(self, text):
|
| 70 |
+
self.assertEqual(text, self.regexp.sub('', text))
|
| 71 |
+
|
| 72 |
+
def check_result(self, text, expected):
|
| 73 |
+
self.assertEqual(expected, self.regexp.sub('', text))
|
| 74 |
+
|
| 75 |
+
def test_empty(self):
|
| 76 |
+
self.all_removed('')
|
| 77 |
+
|
| 78 |
+
def test_newline(self):
|
| 79 |
+
self.all_removed('\n')
|
| 80 |
+
|
| 81 |
+
def test_whitespace_no_newline(self):
|
| 82 |
+
self.all_removed(' ')
|
| 83 |
+
self.all_removed(' ')
|
| 84 |
+
self.all_removed(' ')
|
| 85 |
+
self.all_removed(' ' * 20)
|
| 86 |
+
self.all_removed('\t')
|
| 87 |
+
self.all_removed('\t\t')
|
| 88 |
+
self.all_removed('\t\t\t')
|
| 89 |
+
self.all_removed('\t' * 20)
|
| 90 |
+
self.all_removed('\t ')
|
| 91 |
+
self.all_removed(' \t')
|
| 92 |
+
self.all_removed(' \t \t ')
|
| 93 |
+
self.all_removed('\t \t \t')
|
| 94 |
+
|
| 95 |
+
def test_newline_with_whitespace(self):
|
| 96 |
+
self.all_removed(' \n')
|
| 97 |
+
self.all_removed('\t\n')
|
| 98 |
+
self.all_removed(' \t\n')
|
| 99 |
+
self.all_removed('\t \n')
|
| 100 |
+
self.all_removed('\n ')
|
| 101 |
+
self.all_removed('\n\t')
|
| 102 |
+
self.all_removed('\n \t')
|
| 103 |
+
self.all_removed('\n\t ')
|
| 104 |
+
self.all_removed(' \n ')
|
| 105 |
+
self.all_removed('\t\n ')
|
| 106 |
+
self.all_removed(' \n\t')
|
| 107 |
+
self.all_removed('\t\n\t')
|
| 108 |
+
self.all_removed('\t \t \t\n')
|
| 109 |
+
self.all_removed(' \t \t \n')
|
| 110 |
+
self.all_removed('\n\t \t \t')
|
| 111 |
+
self.all_removed('\n \t \t ')
|
| 112 |
+
|
| 113 |
+
def test_multiple_newlines(self):
|
| 114 |
+
self.check_result('\n\n', '\n')
|
| 115 |
+
self.check_result('\n' * 5, '\n' * 4)
|
| 116 |
+
self.check_result('\n' * 5 + '\t', '\n' * 4)
|
| 117 |
+
self.check_result('\n' * 20, '\n' * 19)
|
| 118 |
+
self.check_result('\n' * 20 + ' ', '\n' * 19)
|
| 119 |
+
self.check_result(' \n \n ', ' \n')
|
| 120 |
+
self.check_result(' \n\n ', ' \n')
|
| 121 |
+
self.check_result(' \n\n', ' \n')
|
| 122 |
+
self.check_result('\t\n\n', '\t\n')
|
| 123 |
+
self.check_result('\n\n ', '\n')
|
| 124 |
+
self.check_result('\n\n\t', '\n')
|
| 125 |
+
self.check_result(' \n \n ', ' \n')
|
| 126 |
+
self.check_result('\t\n\t\n\t', '\t\n')
|
| 127 |
+
|
| 128 |
+
def test_non_whitespace(self):
|
| 129 |
+
self.none_removed('a')
|
| 130 |
+
self.check_result('a\n', 'a')
|
| 131 |
+
self.check_result('a\n ', 'a')
|
| 132 |
+
self.check_result('a \n ', 'a')
|
| 133 |
+
self.check_result('a \n\t', 'a')
|
| 134 |
+
self.none_removed('-')
|
| 135 |
+
self.check_result('-\n', '-')
|
| 136 |
+
self.none_removed('.')
|
| 137 |
+
self.check_result('.\n', '.')
|
| 138 |
+
|
| 139 |
+
def test_unsupported_whitespace(self):
|
| 140 |
+
self.none_removed('\v')
|
| 141 |
+
self.none_removed('\n\v')
|
| 142 |
+
self.check_result('\v\n',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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test query, coverage 93%.
|
| 2 |
+
|
| 3 |
+
Non-gui tests for Query, SectionName, ModuleName, and HelpSource use
|
| 4 |
+
dummy versions that extract the non-gui methods and add other needed
|
| 5 |
+
attributes. GUI tests create an instance of each class and simulate
|
| 6 |
+
entries and button clicks. Subclass tests only target the new code in
|
| 7 |
+
the subclass definition.
|
| 8 |
+
|
| 9 |
+
The appearance of the widgets is checked by the Query and
|
| 10 |
+
HelpSource htests. These are run by running query.py.
|
| 11 |
+
"""
|
| 12 |
+
from idlelib import query
|
| 13 |
+
import unittest
|
| 14 |
+
from test.support import requires
|
| 15 |
+
from tkinter import Tk, END
|
| 16 |
+
|
| 17 |
+
import sys
|
| 18 |
+
from unittest import mock
|
| 19 |
+
from idlelib.idle_test.mock_tk import Var
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# NON-GUI TESTS
|
| 23 |
+
|
| 24 |
+
class QueryTest(unittest.TestCase):
|
| 25 |
+
"Test Query base class."
|
| 26 |
+
|
| 27 |
+
class Dummy_Query:
|
| 28 |
+
# Test the following Query methods.
|
| 29 |
+
entry_ok = query.Query.entry_ok
|
| 30 |
+
ok = query.Query.ok
|
| 31 |
+
cancel = query.Query.cancel
|
| 32 |
+
# Add attributes and initialization needed for tests.
|
| 33 |
+
def __init__(self, dummy_entry):
|
| 34 |
+
self.entry = Var(value=dummy_entry)
|
| 35 |
+
self.entry_error = {'text': ''}
|
| 36 |
+
self.result = None
|
| 37 |
+
self.destroyed = False
|
| 38 |
+
def showerror(self, message):
|
| 39 |
+
self.entry_error['text'] = message
|
| 40 |
+
def destroy(self):
|
| 41 |
+
self.destroyed = True
|
| 42 |
+
|
| 43 |
+
def test_entry_ok_blank(self):
|
| 44 |
+
dialog = self.Dummy_Query(' ')
|
| 45 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 46 |
+
self.assertEqual((dialog.result, dialog.destroyed), (None, False))
|
| 47 |
+
self.assertIn('blank line', dialog.entry_error['text'])
|
| 48 |
+
|
| 49 |
+
def test_entry_ok_good(self):
|
| 50 |
+
dialog = self.Dummy_Query(' good ')
|
| 51 |
+
Equal = self.assertEqual
|
| 52 |
+
Equal(dialog.entry_ok(), 'good')
|
| 53 |
+
Equal((dialog.result, dialog.destroyed), (None, False))
|
| 54 |
+
Equal(dialog.entry_error['text'], '')
|
| 55 |
+
|
| 56 |
+
def test_ok_blank(self):
|
| 57 |
+
dialog = self.Dummy_Query('')
|
| 58 |
+
dialog.entry.focus_set = mock.Mock()
|
| 59 |
+
self.assertEqual(dialog.ok(), None)
|
| 60 |
+
self.assertTrue(dialog.entry.focus_set.called)
|
| 61 |
+
del dialog.entry.focus_set
|
| 62 |
+
self.assertEqual((dialog.result, dialog.destroyed), (None, False))
|
| 63 |
+
|
| 64 |
+
def test_ok_good(self):
|
| 65 |
+
dialog = self.Dummy_Query('good')
|
| 66 |
+
self.assertEqual(dialog.ok(), None)
|
| 67 |
+
self.assertEqual((dialog.result, dialog.destroyed), ('good', True))
|
| 68 |
+
|
| 69 |
+
def test_cancel(self):
|
| 70 |
+
dialog = self.Dummy_Query('does not matter')
|
| 71 |
+
self.assertEqual(dialog.cancel(), None)
|
| 72 |
+
self.assertEqual((dialog.result, dialog.destroyed), (None, True))
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class SectionNameTest(unittest.TestCase):
|
| 76 |
+
"Test SectionName subclass of Query."
|
| 77 |
+
|
| 78 |
+
class Dummy_SectionName:
|
| 79 |
+
entry_ok = query.SectionName.entry_ok # Function being tested.
|
| 80 |
+
used_names = ['used']
|
| 81 |
+
def __init__(self, dummy_entry):
|
| 82 |
+
self.entry = Var(value=dummy_entry)
|
| 83 |
+
self.entry_error = {'text': ''}
|
| 84 |
+
def showerror(self, message):
|
| 85 |
+
self.entry_error['text'] = message
|
| 86 |
+
|
| 87 |
+
def test_blank_section_name(self):
|
| 88 |
+
dialog = self.Dummy_SectionName(' ')
|
| 89 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 90 |
+
self.assertIn('no name', dialog.entry_error['text'])
|
| 91 |
+
|
| 92 |
+
def test_used_section_name(self):
|
| 93 |
+
dialog = self.Dummy_SectionName('used')
|
| 94 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 95 |
+
self.assertIn('use', dialog.entry_error['text'])
|
| 96 |
+
|
| 97 |
+
def test_long_section_name(self):
|
| 98 |
+
dialog = self.Dummy_SectionName('good'*8)
|
| 99 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 100 |
+
self.assertIn('longer than 30', dialog.entry_error['text'])
|
| 101 |
+
|
| 102 |
+
def test_good_section_name(self):
|
| 103 |
+
dialog = self.Dummy_SectionName(' good ')
|
| 104 |
+
self.assertEqual(dialog.entry_ok(), 'good')
|
| 105 |
+
self.assertEqual(dialog.entry_error['text'], '')
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
class ModuleNameTest(unittest.TestCase):
|
| 109 |
+
"Test ModuleName subclass of Query."
|
| 110 |
+
|
| 111 |
+
class Dummy_ModuleName:
|
| 112 |
+
entry_ok = query.ModuleName.entry_ok # Function being tested.
|
| 113 |
+
text0 = ''
|
| 114 |
+
def __init__(self, dummy_entry):
|
| 115 |
+
self.entry = Var(value=dummy_entry)
|
| 116 |
+
self.entry_error = {'text': ''}
|
| 117 |
+
def showerror(self, message):
|
| 118 |
+
self.entry_error['text'] = message
|
| 119 |
+
|
| 120 |
+
def test_blank_module_name(self):
|
| 121 |
+
dialog = self.Dummy_ModuleName(' ')
|
| 122 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 123 |
+
self.assertIn('no name', dialog.entry_error['text'])
|
| 124 |
+
|
| 125 |
+
def test_bogus_module_name(self):
|
| 126 |
+
dialog = self.Dummy_ModuleName('__name_xyz123_should_not_exist__')
|
| 127 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 128 |
+
self.assertIn('not found', dialog.entry_error['text'])
|
| 129 |
+
|
| 130 |
+
def test_c_source_name(self):
|
| 131 |
+
dialog = self.Dummy_ModuleName('itertools')
|
| 132 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 133 |
+
self.assertIn('source-based', dialog.entry_error['text'])
|
| 134 |
+
|
| 135 |
+
def test_good_module_name(self):
|
| 136 |
+
dialog = self.Dummy_ModuleName('idlelib')
|
| 137 |
+
self.assertTrue(dialog.entry_ok().endswith('__init__.py'))
|
| 138 |
+
self.assertEqual(dialog.entry_error['text'], '')
|
| 139 |
+
dialog = self.Dummy_ModuleName('idlelib.idle')
|
| 140 |
+
self.assertTrue(dialog.entry_ok().endswith('idle.py'))
|
| 141 |
+
self.assertEqual(dialog.entry_error['text'], '')
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
class GotoTest(unittest.TestCase):
|
| 145 |
+
"Test Goto subclass of Query."
|
| 146 |
+
|
| 147 |
+
class Dummy_ModuleName:
|
| 148 |
+
entry_ok = query.Goto.entry_ok # Function being tested.
|
| 149 |
+
def __init__(self, dummy_entry):
|
| 150 |
+
self.entry = Var(value=dummy_entry)
|
| 151 |
+
self.entry_error = {'text': ''}
|
| 152 |
+
def showerror(self, message):
|
| 153 |
+
self.entry_error['text'] = message
|
| 154 |
+
|
| 155 |
+
def test_bogus_goto(self):
|
| 156 |
+
dialog = self.Dummy_ModuleName('a')
|
| 157 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 158 |
+
self.assertIn('not a base 10 integer', dialog.entry_error['text'])
|
| 159 |
+
|
| 160 |
+
def test_bad_goto(self):
|
| 161 |
+
dialog = self.Dummy_ModuleName('0')
|
| 162 |
+
self.assertEqual(dialog.entry_ok(), None)
|
| 163 |
+
self.assertIn('not a positive integer', dialog.entry_error['text'])
|
| 164 |
+
|
| 165 |
+
def test_good_goto(self):
|
| 166 |
+
dialog = self.Dummy_ModuleName('1')
|
| 167 |
+
self.assertEqual(dialog.entry_ok(), 1)
|
| 168 |
+
self.assertEqual(dialog.entry_error['text'], '')
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# 3 HelpSource test classes each test one method.
|
| 172 |
+
|
| 173 |
+
class HelpsourceBrowsefileTest(unittest.TestCase):
|
| 174 |
+
"Test browse_file method of ModuleName subclass of Query."
|
| 175 |
+
|
| 176 |
+
class Dummy_HelpSource:
|
| 177 |
+
browse_file = query.HelpSource.browse_file
|
| 178 |
+
pathvar = Var()
|
| 179 |
+
|
| 180 |
+
def test_file_replaces_path(self):
|
| 181 |
+
dialog = self.Dummy_HelpSource()
|
| 182 |
+
# Path is widget entry, either '' or something.
|
| 183 |
+
# Func return is file dialog return, either '' or something.
|
| 184 |
+
# Func return should override widget entry.
|
| 185 |
+
# We need all 4 combinations to test all (most) code paths.
|
| 186 |
+
for path, func, result in (
|
| 187 |
+
('', lambda a,b,c:'', ''),
|
| 188 |
+
('', lambda a,b,c: __file__, __file__),
|
| 189 |
+
('htest', lambda a,b,c:'', 'htest'),
|
| 190 |
+
('htest', lambda a,b,c: __file__, __file__)):
|
| 191 |
+
with self.subTest():
|
| 192 |
+
dialog.pathvar.set(path)
|
| 193 |
+
dialog.askfilename = func
|
| 194 |
+
dialog.browse_file()
|
| 195 |
+
self.assertEqual(dialog.pathvar.get(), result)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
class HelpsourcePathokTest(unittest.TestCase):
|
| 199 |
+
"Test path_ok method of HelpSource subclass of Query."
|
| 200 |
+
|
| 201 |
+
class Dummy_HelpSource:
|
| 202 |
+
path_ok = query.HelpSource.path_ok
|
| 203 |
+
def __init__(self, dummy_path):
|
| 204 |
+
self.path = Var(value=dummy_path)
|
| 205 |
+
self.path_error = {'text': ''}
|
| 206 |
+
def showerror(self, message, widget=None):
|
| 207 |
+
self.path_error['text'] = message
|
| 208 |
+
|
| 209 |
+
orig_platform = query.platform # Set in test_path_ok_file.
|
| 210 |
+
@classmethod
|
| 211 |
+
def tearDownClass(cls):
|
| 212 |
+
query.platform = cls.orig_platform
|
| 213 |
+
|
| 214 |
+
def test_path_ok_blank(self):
|
| 215 |
+
dialog = self.Dummy_HelpSource(' ')
|
| 216 |
+
self.assertEqual(dialog.path_ok(), None)
|
| 217 |
+
self.assertIn('no help file', dialog.path_error['text'])
|
| 218 |
+
|
| 219 |
+
def test_path_ok_bad(self):
|
| 220 |
+
dialog = self.Dummy_HelpSource(__file__ + 'bad-bad-bad')
|
| 221 |
+
self.assertEqual(dialog.path_ok(), None)
|
| 222 |
+
self.assertIn('not exist', dialog.path_error['text'])
|
| 223 |
+
|
| 224 |
+
def test_path_ok_web(self):
|
| 225 |
+
dialog = self.Dummy_HelpSource('')
|
| 226 |
+
Equal = self.assertEqual
|
| 227 |
+
for url in 'www.py.org', 'http://py.org':
|
| 228 |
+
with self.subTest():
|
| 229 |
+
dialog.path.set(url)
|
| 230 |
+
self.assertEqual(dialog.path_ok(), url)
|
| 231 |
+
self.assertEqual(dialog.path_error['text'], '')
|
| 232 |
+
|
| 233 |
+
def test_path_ok_file(self):
|
| 234 |
+
dialog = self.Dummy_HelpSource('')
|
| 235 |
+
for platform, prefix in ('darwin', 'file://'), ('other', ''):
|
| 236 |
+
with self.subTest():
|
| 237 |
+
query.platform = platform
|
| 238 |
+
dialog.path.set(__file__)
|
| 239 |
+
self.assertEqual(dialog.path_ok(), prefix + __file__)
|
| 240 |
+
self.assertEqual(dialog.path_error['text'], '')
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
class HelpsourceEntryokTest(unittest.TestCase):
|
| 244 |
+
"Test entry_ok method of HelpSource subclass of Query."
|
| 245 |
+
|
| 246 |
+
class Dummy_HelpSource:
|
| 247 |
+
entry_ok = query.HelpSource.entry_ok
|
| 248 |
+
entry_error = {}
|
| 249 |
+
path_error = {}
|
| 250 |
+
def item_ok(self):
|
| 251 |
+
return self.name
|
| 252 |
+
def path_ok(self):
|
| 253 |
+
return self.path
|
| 254 |
+
|
| 255 |
+
def test_entry_ok_helpsource(self):
|
| 256 |
+
dialog = self.Dummy_HelpSource()
|
| 257 |
+
for name, path, result in ((None, None, None),
|
| 258 |
+
(None, 'doc.txt', None),
|
| 259 |
+
('doc', None, None),
|
| 260 |
+
('doc', 'doc.txt', ('doc', 'doc.txt'))):
|
| 261 |
+
with self.subTest():
|
| 262 |
+
dialog.name, dialog.path = name, path
|
| 263 |
+
self.assertEqual(dialog.entry_ok(), result)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
# 2 CustomRun test classes each test one method.
|
| 267 |
+
|
| 268 |
+
class CustomRunCLIargsokTest(unittest.TestCase):
|
| 269 |
+
"Test cli_ok method of the CustomRun subclass of Query."
|
| 270 |
+
|
| 271 |
+
class Dummy_CustomRun:
|
| 272 |
+
cli_args_ok = query.CustomRun.cli_args_ok
|
| 273 |
+
def __init__(self, dummy_entry):
|
| 274 |
+
self.entry = Var(value=dummy_entry)
|
| 275 |
+
self.entry_error = {'text': ''}
|
| 276 |
+
def showerror(self, message):
|
| 277 |
+
self.entry_error['text'] = message
|
| 278 |
+
|
| 279 |
+
def test_blank_args(self):
|
| 280 |
+
dialog = self.Dummy_CustomRun(' ')
|
| 281 |
+
self.assertEqual(dialog.cli_args_ok(), [])
|
| 282 |
+
|
| 283 |
+
def test_invalid_args(self):
|
| 284 |
+
dialog = self.Dummy_CustomRun("'no-closing-quote")
|
| 285 |
+
self.assertEqual(dialog.cli_args_ok(), None)
|
| 286 |
+
self.assertIn('No closing', dialog.entry_error['text'])
|
| 287 |
+
|
| 288 |
+
def test_good_args(self):
|
| 289 |
+
args = ['-n', '10', '--verbose', '-p', '/path', '--name']
|
| 290 |
+
dialog = self.Dummy_CustomRun(' '.join(args) + ' "my name"')
|
| 291 |
+
self.assertEqual(dialog.cli_args_ok(), args + ["my name"])
|
| 292 |
+
self.assertEqual(dialog.entry_error['text'], '')
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
class CustomRunEntryokTest(unittest.TestCase):
|
| 296 |
+
"Test entry_ok method of the CustomRun subclass of Query."
|
| 297 |
+
|
| 298 |
+
class Dummy_CustomRun:
|
| 299 |
+
entry_ok = query.CustomRun.entry_ok
|
| 300 |
+
entry_error = {}
|
| 301 |
+
restartvar = Var()
|
| 302 |
+
def cli_args_ok(self):
|
| 303 |
+
return self.cli_args
|
| 304 |
+
|
| 305 |
+
def test_entry_ok_customrun(self):
|
| 306 |
+
dialog = self.Dummy_CustomRun()
|
| 307 |
+
for restart in {True, False}:
|
| 308 |
+
dialog.restartvar.set(restart)
|
| 309 |
+
for cli_args, result in ((None, None),
|
| 310 |
+
(['my arg'], (['my arg'], restart))):
|
| 311 |
+
with self.subTest(restart=restart, cli_args=cli_args):
|
| 312 |
+
dialog.cli_args = cli_args
|
| 313 |
+
self.assertEqual(dialog.entry_ok(), result)
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
# GUI TESTS
|
| 317 |
+
|
| 318 |
+
class QueryGuiTest(unittest.TestCase):
|
| 319 |
+
|
| 320 |
+
@classmethod
|
| 321 |
+
def setUpClass(cls):
|
| 322 |
+
requires('gui')
|
| 323 |
+
cls.root = root = Tk()
|
| 324 |
+
cls.root.withdraw()
|
| 325 |
+
cls.dialog = query.Query(root, 'TEST', 'test', _utest=True)
|
| 326 |
+
cls.dialog.destroy = mock.Mock()
|
| 327 |
+
|
| 328 |
+
@classmethod
|
| 329 |
+
def tearDownClass(cls):
|
| 330 |
+
del cls.dialog.destroy
|
| 331 |
+
del cls.dialog
|
| 332 |
+
cls.root.destroy()
|
| 333 |
+
del cls.root
|
| 334 |
+
|
| 335 |
+
def setUp(self):
|
| 336 |
+
self.dialog.entry.delete(0, 'end')
|
| 337 |
+
self.dialog.result = None
|
| 338 |
+
self.dialog.destroy.reset_mock()
|
| 339 |
+
|
| 340 |
+
def test_click_ok(self):
|
| 341 |
+
dialog = self.dialog
|
| 342 |
+
dialog.entry.insert(0, 'abc')
|
| 343 |
+
dialog.button_ok.invoke()
|
| 344 |
+
self.assertEqual(dialog.result, 'abc')
|
| 345 |
+
self.assertTrue(dialog.destroy.called)
|
| 346 |
+
|
| 347 |
+
def test_click_blank(self):
|
| 348 |
+
dialog = self.dialog
|
| 349 |
+
dialog.button_ok.invoke()
|
| 350 |
+
self.assertEqual(dialog.result, None)
|
| 351 |
+
self.assertFalse(dialog.destroy.called)
|
| 352 |
+
|
| 353 |
+
def test_click_cancel(self):
|
| 354 |
+
dialog = self.dialog
|
| 355 |
+
dialog.entry.insert(0, 'abc')
|
| 356 |
+
dialog.button_cancel.invoke()
|
| 357 |
+
self.assertEqual(dialog.result, None)
|
| 358 |
+
self.assertTrue(dialog.destroy.called)
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
class SectionnameGuiTest(unittest.TestCase):
|
| 362 |
+
|
| 363 |
+
@classmethod
|
| 364 |
+
def setUpClass(cls):
|
| 365 |
+
requires('gui')
|
| 366 |
+
|
| 367 |
+
def test_click_section_name(self):
|
| 368 |
+
root = Tk()
|
| 369 |
+
root.withdraw()
|
| 370 |
+
dialog = query.SectionName(root, 'T', 't', {'abc'}, _utest=True)
|
| 371 |
+
Equal = self.assertEqual
|
| 372 |
+
self.assertEqual(dialog.used_names, {'abc'})
|
| 373 |
+
dialog.entry.insert(0, 'okay')
|
| 374 |
+
dialog.button_ok.invoke()
|
| 375 |
+
self.assertEqual(dialog.result, 'okay')
|
| 376 |
+
root.destroy()
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
class ModulenameGuiTest(unittest.TestCase):
|
| 380 |
+
|
| 381 |
+
@classmethod
|
| 382 |
+
def setUpClass(cls):
|
| 383 |
+
requires('gui')
|
| 384 |
+
|
| 385 |
+
def test_click_module_name(self):
|
| 386 |
+
root = Tk()
|
| 387 |
+
root.withdraw()
|
| 388 |
+
dialog = query.ModuleName(root, 'T', 't', 'idlelib', _utest=True)
|
| 389 |
+
self.assertEqual(dialog.text0, 'idlelib')
|
| 390 |
+
self.assertEqual(dialog.entry.get(), 'idlelib')
|
| 391 |
+
dialog.button_ok.invoke()
|
| 392 |
+
self.assertTrue(dialog.result.endswith('__init__.py'))
|
| 393 |
+
root.destroy()
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
class GotoGuiTest(unittest.TestCase):
|
| 397 |
+
|
| 398 |
+
@classmethod
|
| 399 |
+
def setUpClass(cls):
|
| 400 |
+
requires('gui')
|
| 401 |
+
|
| 402 |
+
def test_click_module_name(self):
|
| 403 |
+
root = Tk()
|
| 404 |
+
root.withdraw()
|
| 405 |
+
dialog = query.Goto(root, 'T', 't', _utest=True)
|
| 406 |
+
dialog.entry.insert(0, '22')
|
| 407 |
+
dialog.button_ok.invoke()
|
| 408 |
+
self.assertEqual(dialog.result, 22)
|
| 409 |
+
root.destroy()
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
class HelpsourceGuiTest(unittest.TestCase):
|
| 413 |
+
|
| 414 |
+
@classmethod
|
| 415 |
+
def setUpClass(cls):
|
| 416 |
+
requires('gui')
|
| 417 |
+
|
| 418 |
+
def test_click_help_source(self):
|
| 419 |
+
root = Tk()
|
| 420 |
+
root.withdraw()
|
| 421 |
+
dialog = query.HelpSource(root, 'T', menuitem='__test__',
|
| 422 |
+
filepath=__file__, _utest=True)
|
| 423 |
+
Equal = self.assertEqual
|
| 424 |
+
Equal(dialog.entry.get(), '__test__')
|
| 425 |
+
Equal(dialog.path.get(), __file__)
|
| 426 |
+
dialog.button_ok.invoke()
|
| 427 |
+
prefix = "file://" if sys.platform == 'darwin' else ''
|
| 428 |
+
Equal(dialog.result, ('__test__', prefix + __file__))
|
| 429 |
+
root.destroy()
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
class CustomRunGuiTest(unittest.TestCase):
|
| 433 |
+
|
| 434 |
+
@classmethod
|
| 435 |
+
def setUpClass(cls):
|
| 436 |
+
requires('gui')
|
| 437 |
+
|
| 438 |
+
def test_click_args(self):
|
| 439 |
+
root = Tk()
|
| 440 |
+
root.withdraw()
|
| 441 |
+
dialog = query.CustomRun(root, 'Title',
|
| 442 |
+
cli_args=['a', 'b=1'], _utest=True)
|
| 443 |
+
self.assertEqual(dialog.entry.get(), 'a b=1')
|
| 444 |
+
dialog.entry.insert(END, ' c')
|
| 445 |
+
dialog.button_ok.invoke()
|
| 446 |
+
self.assertEqual(dialog.result, (['a', 'b=1', 'c'], True))
|
| 447 |
+
root.destroy()
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
if __name__ == '__main__':
|
| 451 |
+
unittest.main(verbosity=2, exit=False)
|
llava/lib/python3.10/idlelib/idle_test/test_redirector.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test redirector, coverage 100%."
|
| 2 |
+
|
| 3 |
+
from idlelib.redirector import WidgetRedirector
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
from tkinter import Tk, Text, TclError
|
| 7 |
+
from idlelib.idle_test.mock_idle import Func
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class InitCloseTest(unittest.TestCase):
|
| 11 |
+
|
| 12 |
+
@classmethod
|
| 13 |
+
def setUpClass(cls):
|
| 14 |
+
requires('gui')
|
| 15 |
+
cls.root = Tk()
|
| 16 |
+
cls.root.withdraw()
|
| 17 |
+
cls.text = Text(cls.root)
|
| 18 |
+
|
| 19 |
+
@classmethod
|
| 20 |
+
def tearDownClass(cls):
|
| 21 |
+
del cls.text
|
| 22 |
+
cls.root.destroy()
|
| 23 |
+
del cls.root
|
| 24 |
+
|
| 25 |
+
def test_init(self):
|
| 26 |
+
redir = WidgetRedirector(self.text)
|
| 27 |
+
self.assertEqual(redir.widget, self.text)
|
| 28 |
+
self.assertEqual(redir.tk, self.text.tk)
|
| 29 |
+
self.assertRaises(TclError, WidgetRedirector, self.text)
|
| 30 |
+
redir.close() # restore self.tk, self.text
|
| 31 |
+
|
| 32 |
+
def test_close(self):
|
| 33 |
+
redir = WidgetRedirector(self.text)
|
| 34 |
+
redir.register('insert', Func)
|
| 35 |
+
redir.close()
|
| 36 |
+
self.assertEqual(redir._operations, {})
|
| 37 |
+
self.assertFalse(hasattr(self.text, 'widget'))
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class WidgetRedirectorTest(unittest.TestCase):
|
| 41 |
+
|
| 42 |
+
@classmethod
|
| 43 |
+
def setUpClass(cls):
|
| 44 |
+
requires('gui')
|
| 45 |
+
cls.root = Tk()
|
| 46 |
+
cls.root.withdraw()
|
| 47 |
+
cls.text = Text(cls.root)
|
| 48 |
+
|
| 49 |
+
@classmethod
|
| 50 |
+
def tearDownClass(cls):
|
| 51 |
+
del cls.text
|
| 52 |
+
cls.root.update_idletasks()
|
| 53 |
+
cls.root.destroy()
|
| 54 |
+
del cls.root
|
| 55 |
+
|
| 56 |
+
def setUp(self):
|
| 57 |
+
self.redir = WidgetRedirector(self.text)
|
| 58 |
+
self.func = Func()
|
| 59 |
+
self.orig_insert = self.redir.register('insert', self.func)
|
| 60 |
+
self.text.insert('insert', 'asdf') # leaves self.text empty
|
| 61 |
+
|
| 62 |
+
def tearDown(self):
|
| 63 |
+
self.text.delete('1.0', 'end')
|
| 64 |
+
self.redir.close()
|
| 65 |
+
|
| 66 |
+
def test_repr(self): # partly for 100% coverage
|
| 67 |
+
self.assertIn('Redirector', repr(self.redir))
|
| 68 |
+
self.assertIn('Original', repr(self.orig_insert))
|
| 69 |
+
|
| 70 |
+
def test_register(self):
|
| 71 |
+
self.assertEqual(self.text.get('1.0', 'end'), '\n')
|
| 72 |
+
self.assertEqual(self.func.args, ('insert', 'asdf'))
|
| 73 |
+
self.assertIn('insert', self.redir._operations)
|
| 74 |
+
self.assertIn('insert', self.text.__dict__)
|
| 75 |
+
self.assertEqual(self.text.insert, self.func)
|
| 76 |
+
|
| 77 |
+
def test_original_command(self):
|
| 78 |
+
self.assertEqual(self.orig_insert.operation, 'insert')
|
| 79 |
+
self.assertEqual(self.orig_insert.tk_call, self.text.tk.call)
|
| 80 |
+
self.orig_insert('insert', 'asdf')
|
| 81 |
+
self.assertEqual(self.text.get('1.0', 'end'), 'asdf\n')
|
| 82 |
+
|
| 83 |
+
def test_unregister(self):
|
| 84 |
+
self.assertIsNone(self.redir.unregister('invalid operation name'))
|
| 85 |
+
self.assertEqual(self.redir.unregister('insert'), self.func)
|
| 86 |
+
self.assertNotIn('insert', self.redir._operations)
|
| 87 |
+
self.assertNotIn('insert', self.text.__dict__)
|
| 88 |
+
|
| 89 |
+
def test_unregister_no_attribute(self):
|
| 90 |
+
del self.text.insert
|
| 91 |
+
self.assertEqual(self.redir.unregister('insert'), self.func)
|
| 92 |
+
|
| 93 |
+
def test_dispatch_intercept(self):
|
| 94 |
+
self.func.__init__(True)
|
| 95 |
+
self.assertTrue(self.redir.dispatch('insert', False))
|
| 96 |
+
self.assertFalse(self.func.args[0])
|
| 97 |
+
|
| 98 |
+
def test_dispatch_bypass(self):
|
| 99 |
+
self.orig_insert('insert', 'asdf')
|
| 100 |
+
# tk.call returns '' where Python would return None
|
| 101 |
+
self.assertEqual(self.redir.dispatch('delete', '1.0', 'end'), '')
|
| 102 |
+
self.assertEqual(self.text.get('1.0', 'end'), '\n')
|
| 103 |
+
|
| 104 |
+
def test_dispatch_error(self):
|
| 105 |
+
self.func.__init__(TclError())
|
| 106 |
+
self.assertEqual(self.redir.dispatch('insert', False), '')
|
| 107 |
+
self.assertEqual(self.redir.dispatch('invalid'), '')
|
| 108 |
+
|
| 109 |
+
def test_command_dispatch(self):
|
| 110 |
+
# Test that .__init__ causes redirection of tk calls
|
| 111 |
+
# through redir.dispatch
|
| 112 |
+
self.root.call(self.text._w, 'insert', 'hello')
|
| 113 |
+
self.assertEqual(self.func.args, ('hello',))
|
| 114 |
+
self.assertEqual(self.text.get('1.0', 'end'), '\n')
|
| 115 |
+
# Ensure that called through redir .dispatch and not through
|
| 116 |
+
# self.text.insert by having mock raise TclError.
|
| 117 |
+
self.func.__init__(TclError())
|
| 118 |
+
self.assertEqual(self.root.call(self.text._w, 'insert', 'boo'), '')
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == '__main__':
|
| 122 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_replace.py
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test replace, coverage 78%."
|
| 2 |
+
|
| 3 |
+
from idlelib.replace import ReplaceDialog
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
requires('gui')
|
| 7 |
+
from tkinter import Tk, Text
|
| 8 |
+
|
| 9 |
+
from unittest.mock import Mock
|
| 10 |
+
from idlelib.idle_test.mock_tk import Mbox
|
| 11 |
+
import idlelib.searchengine as se
|
| 12 |
+
|
| 13 |
+
orig_mbox = se.messagebox
|
| 14 |
+
showerror = Mbox.showerror
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ReplaceDialogTest(unittest.TestCase):
|
| 18 |
+
|
| 19 |
+
@classmethod
|
| 20 |
+
def setUpClass(cls):
|
| 21 |
+
cls.root = Tk()
|
| 22 |
+
cls.root.withdraw()
|
| 23 |
+
se.messagebox = Mbox
|
| 24 |
+
cls.engine = se.SearchEngine(cls.root)
|
| 25 |
+
cls.dialog = ReplaceDialog(cls.root, cls.engine)
|
| 26 |
+
cls.dialog.bell = lambda: None
|
| 27 |
+
cls.dialog.ok = Mock()
|
| 28 |
+
cls.text = Text(cls.root)
|
| 29 |
+
cls.text.undo_block_start = Mock()
|
| 30 |
+
cls.text.undo_block_stop = Mock()
|
| 31 |
+
cls.dialog.text = cls.text
|
| 32 |
+
|
| 33 |
+
@classmethod
|
| 34 |
+
def tearDownClass(cls):
|
| 35 |
+
se.messagebox = orig_mbox
|
| 36 |
+
del cls.text, cls.dialog, cls.engine
|
| 37 |
+
cls.root.destroy()
|
| 38 |
+
del cls.root
|
| 39 |
+
|
| 40 |
+
def setUp(self):
|
| 41 |
+
self.text.insert('insert', 'This is a sample sTring')
|
| 42 |
+
|
| 43 |
+
def tearDown(self):
|
| 44 |
+
self.engine.patvar.set('')
|
| 45 |
+
self.dialog.replvar.set('')
|
| 46 |
+
self.engine.wordvar.set(False)
|
| 47 |
+
self.engine.casevar.set(False)
|
| 48 |
+
self.engine.revar.set(False)
|
| 49 |
+
self.engine.wrapvar.set(True)
|
| 50 |
+
self.engine.backvar.set(False)
|
| 51 |
+
showerror.title = ''
|
| 52 |
+
showerror.message = ''
|
| 53 |
+
self.text.delete('1.0', 'end')
|
| 54 |
+
|
| 55 |
+
def test_replace_simple(self):
|
| 56 |
+
# Test replace function with all options at default setting.
|
| 57 |
+
# Wrap around - True
|
| 58 |
+
# Regular Expression - False
|
| 59 |
+
# Match case - False
|
| 60 |
+
# Match word - False
|
| 61 |
+
# Direction - Forwards
|
| 62 |
+
text = self.text
|
| 63 |
+
equal = self.assertEqual
|
| 64 |
+
pv = self.engine.patvar
|
| 65 |
+
rv = self.dialog.replvar
|
| 66 |
+
replace = self.dialog.replace_it
|
| 67 |
+
|
| 68 |
+
# test accessor method
|
| 69 |
+
self.engine.setpat('asdf')
|
| 70 |
+
equal(self.engine.getpat(), pv.get())
|
| 71 |
+
|
| 72 |
+
# text found and replaced
|
| 73 |
+
pv.set('a')
|
| 74 |
+
rv.set('asdf')
|
| 75 |
+
replace()
|
| 76 |
+
equal(text.get('1.8', '1.12'), 'asdf')
|
| 77 |
+
|
| 78 |
+
# don't "match word" case
|
| 79 |
+
text.mark_set('insert', '1.0')
|
| 80 |
+
pv.set('is')
|
| 81 |
+
rv.set('hello')
|
| 82 |
+
replace()
|
| 83 |
+
equal(text.get('1.2', '1.7'), 'hello')
|
| 84 |
+
|
| 85 |
+
# don't "match case" case
|
| 86 |
+
pv.set('string')
|
| 87 |
+
rv.set('world')
|
| 88 |
+
replace()
|
| 89 |
+
equal(text.get('1.23', '1.28'), 'world')
|
| 90 |
+
|
| 91 |
+
# without "regular expression" case
|
| 92 |
+
text.mark_set('insert', 'end')
|
| 93 |
+
text.insert('insert', '\nline42:')
|
| 94 |
+
before_text = text.get('1.0', 'end')
|
| 95 |
+
pv.set(r'[a-z][\d]+')
|
| 96 |
+
replace()
|
| 97 |
+
after_text = text.get('1.0', 'end')
|
| 98 |
+
equal(before_text, after_text)
|
| 99 |
+
|
| 100 |
+
# test with wrap around selected and complete a cycle
|
| 101 |
+
text.mark_set('insert', '1.9')
|
| 102 |
+
pv.set('i')
|
| 103 |
+
rv.set('j')
|
| 104 |
+
replace()
|
| 105 |
+
equal(text.get('1.8'), 'i')
|
| 106 |
+
equal(text.get('2.1'), 'j')
|
| 107 |
+
replace()
|
| 108 |
+
equal(text.get('2.1'), 'j')
|
| 109 |
+
equal(text.get('1.8'), 'j')
|
| 110 |
+
before_text = text.get('1.0', 'end')
|
| 111 |
+
replace()
|
| 112 |
+
after_text = text.get('1.0', 'end')
|
| 113 |
+
equal(before_text, after_text)
|
| 114 |
+
|
| 115 |
+
# text not found
|
| 116 |
+
before_text = text.get('1.0', 'end')
|
| 117 |
+
pv.set('foobar')
|
| 118 |
+
replace()
|
| 119 |
+
after_text = text.get('1.0', 'end')
|
| 120 |
+
equal(before_text, after_text)
|
| 121 |
+
|
| 122 |
+
# test access method
|
| 123 |
+
self.dialog.find_it(0)
|
| 124 |
+
|
| 125 |
+
def test_replace_wrap_around(self):
|
| 126 |
+
text = self.text
|
| 127 |
+
equal = self.assertEqual
|
| 128 |
+
pv = self.engine.patvar
|
| 129 |
+
rv = self.dialog.replvar
|
| 130 |
+
replace = self.dialog.replace_it
|
| 131 |
+
self.engine.wrapvar.set(False)
|
| 132 |
+
|
| 133 |
+
# replace candidate found both after and before 'insert'
|
| 134 |
+
text.mark_set('insert', '1.4')
|
| 135 |
+
pv.set('i')
|
| 136 |
+
rv.set('j')
|
| 137 |
+
replace()
|
| 138 |
+
equal(text.get('1.2'), 'i')
|
| 139 |
+
equal(text.get('1.5'), 'j')
|
| 140 |
+
replace()
|
| 141 |
+
equal(text.get('1.2'), 'i')
|
| 142 |
+
equal(text.get('1.20'), 'j')
|
| 143 |
+
replace()
|
| 144 |
+
equal(text.get('1.2'), 'i')
|
| 145 |
+
|
| 146 |
+
# replace candidate found only before 'insert'
|
| 147 |
+
text.mark_set('insert', '1.8')
|
| 148 |
+
pv.set('is')
|
| 149 |
+
before_text = text.get('1.0', 'end')
|
| 150 |
+
replace()
|
| 151 |
+
after_text = text.get('1.0', 'end')
|
| 152 |
+
equal(before_text, after_text)
|
| 153 |
+
|
| 154 |
+
def test_replace_whole_word(self):
|
| 155 |
+
text = self.text
|
| 156 |
+
equal = self.assertEqual
|
| 157 |
+
pv = self.engine.patvar
|
| 158 |
+
rv = self.dialog.replvar
|
| 159 |
+
replace = self.dialog.replace_it
|
| 160 |
+
self.engine.wordvar.set(True)
|
| 161 |
+
|
| 162 |
+
pv.set('is')
|
| 163 |
+
rv.set('hello')
|
| 164 |
+
replace()
|
| 165 |
+
equal(text.get('1.0', '1.4'), 'This')
|
| 166 |
+
equal(text.get('1.5', '1.10'), 'hello')
|
| 167 |
+
|
| 168 |
+
def test_replace_match_case(self):
|
| 169 |
+
equal = self.assertEqual
|
| 170 |
+
text = self.text
|
| 171 |
+
pv = self.engine.patvar
|
| 172 |
+
rv = self.dialog.replvar
|
| 173 |
+
replace = self.dialog.replace_it
|
| 174 |
+
self.engine.casevar.set(True)
|
| 175 |
+
|
| 176 |
+
before_text = self.text.get('1.0', 'end')
|
| 177 |
+
pv.set('this')
|
| 178 |
+
rv.set('that')
|
| 179 |
+
replace()
|
| 180 |
+
after_text = self.text.get('1.0', 'end')
|
| 181 |
+
equal(before_text, after_text)
|
| 182 |
+
|
| 183 |
+
pv.set('This')
|
| 184 |
+
replace()
|
| 185 |
+
equal(text.get('1.0', '1.4'), 'that')
|
| 186 |
+
|
| 187 |
+
def test_replace_regex(self):
|
| 188 |
+
equal = self.assertEqual
|
| 189 |
+
text = self.text
|
| 190 |
+
pv = self.engine.patvar
|
| 191 |
+
rv = self.dialog.replvar
|
| 192 |
+
replace = self.dialog.replace_it
|
| 193 |
+
self.engine.revar.set(True)
|
| 194 |
+
|
| 195 |
+
before_text = text.get('1.0', 'end')
|
| 196 |
+
pv.set(r'[a-z][\d]+')
|
| 197 |
+
rv.set('hello')
|
| 198 |
+
replace()
|
| 199 |
+
after_text = text.get('1.0', 'end')
|
| 200 |
+
equal(before_text, after_text)
|
| 201 |
+
|
| 202 |
+
text.insert('insert', '\nline42')
|
| 203 |
+
replace()
|
| 204 |
+
equal(text.get('2.0', '2.8'), 'linhello')
|
| 205 |
+
|
| 206 |
+
pv.set('')
|
| 207 |
+
replace()
|
| 208 |
+
self.assertIn('error', showerror.title)
|
| 209 |
+
self.assertIn('Empty', showerror.message)
|
| 210 |
+
|
| 211 |
+
pv.set(r'[\d')
|
| 212 |
+
replace()
|
| 213 |
+
self.assertIn('error', showerror.title)
|
| 214 |
+
self.assertIn('Pattern', showerror.message)
|
| 215 |
+
|
| 216 |
+
showerror.title = ''
|
| 217 |
+
showerror.message = ''
|
| 218 |
+
pv.set('[a]')
|
| 219 |
+
rv.set('test\\')
|
| 220 |
+
replace()
|
| 221 |
+
self.assertIn('error', showerror.title)
|
| 222 |
+
self.assertIn('Invalid Replace Expression', showerror.message)
|
| 223 |
+
|
| 224 |
+
# test access method
|
| 225 |
+
self.engine.setcookedpat("?")
|
| 226 |
+
equal(pv.get(), "\\?")
|
| 227 |
+
|
| 228 |
+
def test_replace_backwards(self):
|
| 229 |
+
equal = self.assertEqual
|
| 230 |
+
text = self.text
|
| 231 |
+
pv = self.engine.patvar
|
| 232 |
+
rv = self.dialog.replvar
|
| 233 |
+
replace = self.dialog.replace_it
|
| 234 |
+
self.engine.backvar.set(True)
|
| 235 |
+
|
| 236 |
+
text.insert('insert', '\nis as ')
|
| 237 |
+
|
| 238 |
+
pv.set('is')
|
| 239 |
+
rv.set('was')
|
| 240 |
+
replace()
|
| 241 |
+
equal(text.get('1.2', '1.4'), 'is')
|
| 242 |
+
equal(text.get('2.0', '2.3'), 'was')
|
| 243 |
+
replace()
|
| 244 |
+
equal(text.get('1.5', '1.8'), 'was')
|
| 245 |
+
replace()
|
| 246 |
+
equal(text.get('1.2', '1.5'), 'was')
|
| 247 |
+
|
| 248 |
+
def test_replace_all(self):
|
| 249 |
+
text = self.text
|
| 250 |
+
pv = self.engine.patvar
|
| 251 |
+
rv = self.dialog.replvar
|
| 252 |
+
replace_all = self.dialog.replace_all
|
| 253 |
+
|
| 254 |
+
text.insert('insert', '\n')
|
| 255 |
+
text.insert('insert', text.get('1.0', 'end')*100)
|
| 256 |
+
pv.set('is')
|
| 257 |
+
rv.set('was')
|
| 258 |
+
replace_all()
|
| 259 |
+
self.assertNotIn('is', text.get('1.0', 'end'))
|
| 260 |
+
|
| 261 |
+
self.engine.revar.set(True)
|
| 262 |
+
pv.set('')
|
| 263 |
+
replace_all()
|
| 264 |
+
self.assertIn('error', showerror.title)
|
| 265 |
+
self.assertIn('Empty', showerror.message)
|
| 266 |
+
|
| 267 |
+
pv.set('[s][T]')
|
| 268 |
+
rv.set('\\')
|
| 269 |
+
replace_all()
|
| 270 |
+
|
| 271 |
+
self.engine.revar.set(False)
|
| 272 |
+
pv.set('text which is not present')
|
| 273 |
+
rv.set('foobar')
|
| 274 |
+
replace_all()
|
| 275 |
+
|
| 276 |
+
def test_default_command(self):
|
| 277 |
+
text = self.text
|
| 278 |
+
pv = self.engine.patvar
|
| 279 |
+
rv = self.dialog.replvar
|
| 280 |
+
replace_find = self.dialog.default_command
|
| 281 |
+
equal = self.assertEqual
|
| 282 |
+
|
| 283 |
+
pv.set('This')
|
| 284 |
+
rv.set('was')
|
| 285 |
+
replace_find()
|
| 286 |
+
equal(text.get('sel.first', 'sel.last'), 'was')
|
| 287 |
+
|
| 288 |
+
self.engine.revar.set(True)
|
| 289 |
+
pv.set('')
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test run, coverage 54%."
|
| 2 |
+
|
| 3 |
+
from idlelib import run
|
| 4 |
+
import io
|
| 5 |
+
import sys
|
| 6 |
+
from test.support import captured_output, captured_stderr
|
| 7 |
+
import unittest
|
| 8 |
+
from unittest import mock
|
| 9 |
+
import idlelib
|
| 10 |
+
from idlelib.idle_test.mock_idle import Func
|
| 11 |
+
|
| 12 |
+
idlelib.testing = True # Use {} for executing test user code.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class ExceptionTest(unittest.TestCase):
|
| 16 |
+
|
| 17 |
+
def test_print_exception_unhashable(self):
|
| 18 |
+
class UnhashableException(Exception):
|
| 19 |
+
def __eq__(self, other):
|
| 20 |
+
return True
|
| 21 |
+
|
| 22 |
+
ex1 = UnhashableException('ex1')
|
| 23 |
+
ex2 = UnhashableException('ex2')
|
| 24 |
+
try:
|
| 25 |
+
raise ex2 from ex1
|
| 26 |
+
except UnhashableException:
|
| 27 |
+
try:
|
| 28 |
+
raise ex1
|
| 29 |
+
except UnhashableException:
|
| 30 |
+
with captured_stderr() as output:
|
| 31 |
+
with mock.patch.object(run, 'cleanup_traceback') as ct:
|
| 32 |
+
ct.side_effect = lambda t, e: t
|
| 33 |
+
run.print_exception()
|
| 34 |
+
|
| 35 |
+
tb = output.getvalue().strip().splitlines()
|
| 36 |
+
self.assertEqual(11, len(tb))
|
| 37 |
+
self.assertIn('UnhashableException: ex2', tb[3])
|
| 38 |
+
self.assertIn('UnhashableException: ex1', tb[10])
|
| 39 |
+
|
| 40 |
+
data = (('1/0', ZeroDivisionError, "division by zero\n"),
|
| 41 |
+
('abc', NameError, "name 'abc' is not defined. "
|
| 42 |
+
"Did you mean: 'abs'?\n"),
|
| 43 |
+
('int.reel', AttributeError,
|
| 44 |
+
"type object 'int' has no attribute 'reel'. "
|
| 45 |
+
"Did you mean: 'real'?\n"),
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def test_get_message(self):
|
| 49 |
+
for code, exc, msg in self.data:
|
| 50 |
+
with self.subTest(code=code):
|
| 51 |
+
try:
|
| 52 |
+
eval(compile(code, '', 'eval'))
|
| 53 |
+
except exc:
|
| 54 |
+
typ, val, tb = sys.exc_info()
|
| 55 |
+
actual = run.get_message_lines(typ, val, tb)[0]
|
| 56 |
+
expect = f'{exc.__name__}: {msg}'
|
| 57 |
+
self.assertEqual(actual, expect)
|
| 58 |
+
|
| 59 |
+
@mock.patch.object(run, 'cleanup_traceback',
|
| 60 |
+
new_callable=lambda: (lambda t, e: None))
|
| 61 |
+
def test_get_multiple_message(self, mock):
|
| 62 |
+
d = self.data
|
| 63 |
+
data2 = ((d[0], d[1]), (d[1], d[2]), (d[2], d[0]))
|
| 64 |
+
subtests = 0
|
| 65 |
+
for (code1, exc1, msg1), (code2, exc2, msg2) in data2:
|
| 66 |
+
with self.subTest(codes=(code1,code2)):
|
| 67 |
+
try:
|
| 68 |
+
eval(compile(code1, '', 'eval'))
|
| 69 |
+
except exc1:
|
| 70 |
+
try:
|
| 71 |
+
eval(compile(code2, '', 'eval'))
|
| 72 |
+
except exc2:
|
| 73 |
+
with captured_stderr() as output:
|
| 74 |
+
run.print_exception()
|
| 75 |
+
actual = output.getvalue()
|
| 76 |
+
self.assertIn(msg1, actual)
|
| 77 |
+
self.assertIn(msg2, actual)
|
| 78 |
+
subtests += 1
|
| 79 |
+
self.assertEqual(subtests, len(data2)) # All subtests ran?
|
| 80 |
+
|
| 81 |
+
# StdioFile tests.
|
| 82 |
+
|
| 83 |
+
class S(str):
|
| 84 |
+
def __str__(self):
|
| 85 |
+
return '%s:str' % type(self).__name__
|
| 86 |
+
def __unicode__(self):
|
| 87 |
+
return '%s:unicode' % type(self).__name__
|
| 88 |
+
def __len__(self):
|
| 89 |
+
return 3
|
| 90 |
+
def __iter__(self):
|
| 91 |
+
return iter('abc')
|
| 92 |
+
def __getitem__(self, *args):
|
| 93 |
+
return '%s:item' % type(self).__name__
|
| 94 |
+
def __getslice__(self, *args):
|
| 95 |
+
return '%s:slice' % type(self).__name__
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class MockShell:
|
| 99 |
+
def __init__(self):
|
| 100 |
+
self.reset()
|
| 101 |
+
def write(self, *args):
|
| 102 |
+
self.written.append(args)
|
| 103 |
+
def readline(self):
|
| 104 |
+
return self.lines.pop()
|
| 105 |
+
def close(self):
|
| 106 |
+
pass
|
| 107 |
+
def reset(self):
|
| 108 |
+
self.written = []
|
| 109 |
+
def push(self, lines):
|
| 110 |
+
self.lines = list(lines)[::-1]
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
class StdInputFilesTest(unittest.TestCase):
|
| 114 |
+
|
| 115 |
+
def test_misc(self):
|
| 116 |
+
shell = MockShell()
|
| 117 |
+
f = run.StdInputFile(shell, 'stdin')
|
| 118 |
+
self.assertIsInstance(f, io.TextIOBase)
|
| 119 |
+
self.assertEqual(f.encoding, 'utf-8')
|
| 120 |
+
self.assertEqual(f.errors, 'strict')
|
| 121 |
+
self.assertIsNone(f.newlines)
|
| 122 |
+
self.assertEqual(f.name, '<stdin>')
|
| 123 |
+
self.assertFalse(f.closed)
|
| 124 |
+
self.assertTrue(f.isatty())
|
| 125 |
+
self.assertTrue(f.readable())
|
| 126 |
+
self.assertFalse(f.writable())
|
| 127 |
+
self.assertFalse(f.seekable())
|
| 128 |
+
|
| 129 |
+
def test_unsupported(self):
|
| 130 |
+
shell = MockShell()
|
| 131 |
+
f = run.StdInputFile(shell, 'stdin')
|
| 132 |
+
self.assertRaises(OSError, f.fileno)
|
| 133 |
+
self.assertRaises(OSError, f.tell)
|
| 134 |
+
self.assertRaises(OSError, f.seek, 0)
|
| 135 |
+
self.assertRaises(OSError, f.write, 'x')
|
| 136 |
+
self.assertRaises(OSError, f.writelines, ['x'])
|
| 137 |
+
|
| 138 |
+
def test_read(self):
|
| 139 |
+
shell = MockShell()
|
| 140 |
+
f = run.StdInputFile(shell, 'stdin')
|
| 141 |
+
shell.push(['one\n', 'two\n', ''])
|
| 142 |
+
self.assertEqual(f.read(), 'one\ntwo\n')
|
| 143 |
+
shell.push(['one\n', 'two\n', ''])
|
| 144 |
+
self.assertEqual(f.read(-1), 'one\ntwo\n')
|
| 145 |
+
shell.push(['one\n', 'two\n', ''])
|
| 146 |
+
self.assertEqual(f.read(None), 'one\ntwo\n')
|
| 147 |
+
shell.push(['one\n', 'two\n', 'three\n', ''])
|
| 148 |
+
self.assertEqual(f.read(2), 'on')
|
| 149 |
+
self.assertEqual(f.read(3), 'e\nt')
|
| 150 |
+
self.assertEqual(f.read(10), 'wo\nthree\n')
|
| 151 |
+
|
| 152 |
+
shell.push(['one\n', 'two\n'])
|
| 153 |
+
self.assertEqual(f.read(0), '')
|
| 154 |
+
self.assertRaises(TypeError, f.read, 1.5)
|
| 155 |
+
self.assertRaises(TypeError, f.read, '1')
|
| 156 |
+
self.assertRaises(TypeError, f.read, 1, 1)
|
| 157 |
+
|
| 158 |
+
def test_readline(self):
|
| 159 |
+
shell = MockShell()
|
| 160 |
+
f = run.StdInputFile(shell, 'stdin')
|
| 161 |
+
shell.push(['one\n', 'two\n', 'three\n', 'four\n'])
|
| 162 |
+
self.assertEqual(f.readline(), 'one\n')
|
| 163 |
+
self.assertEqual(f.readline(-1), 'two\n')
|
| 164 |
+
self.assertEqual(f.readline(None), 'three\n')
|
| 165 |
+
shell.push(['one\ntwo\n'])
|
| 166 |
+
self.assertEqual(f.readline(), 'one\n')
|
| 167 |
+
self.assertEqual(f.readline(), 'two\n')
|
| 168 |
+
shell.push(['one', 'two', 'three'])
|
| 169 |
+
self.assertEqual(f.readline(), 'one')
|
| 170 |
+
self.assertEqual(f.readline(), 'two')
|
| 171 |
+
shell.push(['one\n', 'two\n', 'three\n'])
|
| 172 |
+
self.assertEqual(f.readline(2), 'on')
|
| 173 |
+
self.assertEqual(f.readline(1), 'e')
|
| 174 |
+
self.assertEqual(f.readline(1), '\n')
|
| 175 |
+
self.assertEqual(f.readline(10), 'two\n')
|
| 176 |
+
|
| 177 |
+
shell.push(['one\n', 'two\n'])
|
| 178 |
+
self.assertEqual(f.readline(0), '')
|
| 179 |
+
self.assertRaises(TypeError, f.readlines, 1.5)
|
| 180 |
+
self.assertRaises(TypeError, f.readlines, '1')
|
| 181 |
+
self.assertRaises(TypeError, f.readlines, 1, 1)
|
| 182 |
+
|
| 183 |
+
def test_readlines(self):
|
| 184 |
+
shell = MockShell()
|
| 185 |
+
f = run.StdInputFile(shell, 'stdin')
|
| 186 |
+
shell.push(['one\n', 'two\n', ''])
|
| 187 |
+
self.assertEqual(f.readlines(), ['one\n', 'two\n'])
|
| 188 |
+
shell.push(['one\n', 'two\n', ''])
|
| 189 |
+
self.assertEqual(f.readlines(-1), ['one\n', 'two\n'])
|
| 190 |
+
shell.push(['one\n', 'two\n', ''])
|
| 191 |
+
self.assertEqual(f.readlines(None), ['one\n', 'two\n'])
|
| 192 |
+
shell.push(['one\n', 'two\n', ''])
|
| 193 |
+
self.assertEqual(f.readlines(0), ['one\n', 'two\n'])
|
| 194 |
+
shell.push(['one\n', 'two\n', ''])
|
| 195 |
+
self.assertEqual(f.readlines(3), ['one\n'])
|
| 196 |
+
shell.push(['one\n', 'two\n', ''])
|
| 197 |
+
self.assertEqual(f.readlines(4), ['one\n', 'two\n'])
|
| 198 |
+
|
| 199 |
+
shell.push(['one\n', 'two\n', ''])
|
| 200 |
+
self.assertRaises(TypeError, f.readlines, 1.5)
|
| 201 |
+
self.assertRaises(TypeError, f.readlines, '1')
|
| 202 |
+
self.assertRaises(TypeError, f.readlines, 1, 1)
|
| 203 |
+
|
| 204 |
+
def test_close(self):
|
| 205 |
+
shell = MockShell()
|
| 206 |
+
f = run.StdInputFile(shell, 'stdin')
|
| 207 |
+
shell.push(['one\n', 'two\n', ''])
|
| 208 |
+
self.assertFalse(f.closed)
|
| 209 |
+
self.assertEqual(f.readline(), 'one\n')
|
| 210 |
+
f.close()
|
| 211 |
+
self.assertFalse(f.closed)
|
| 212 |
+
self.assertEqual(f.readline(), 'two\n')
|
| 213 |
+
self.assertRaises(TypeError, f.close, 1)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class StdOutputFilesTest(unittest.TestCase):
|
| 217 |
+
|
| 218 |
+
def test_misc(self):
|
| 219 |
+
shell = MockShell()
|
| 220 |
+
f = run.StdOutputFile(shell, 'stdout')
|
| 221 |
+
self.assertIsInstance(f, io.TextIOBase)
|
| 222 |
+
self.assertEqual(f.encoding, 'utf-8')
|
| 223 |
+
self.assertEqual(f.errors, 'strict')
|
| 224 |
+
self.assertIsNone(f.newlines)
|
| 225 |
+
self.assertEqual(f.name, '<stdout>')
|
| 226 |
+
self.assertFalse(f.closed)
|
| 227 |
+
self.assertTrue(f.isatty())
|
| 228 |
+
self.assertFalse(f.readable())
|
| 229 |
+
self.assertTrue(f.writable())
|
| 230 |
+
self.assertFalse(f.seekable())
|
| 231 |
+
|
| 232 |
+
def test_unsupported(self):
|
| 233 |
+
shell = MockShell()
|
| 234 |
+
f = run.StdOutputFile(shell, 'stdout')
|
| 235 |
+
self.assertRaises(OSError, f.fileno)
|
| 236 |
+
self.assertRaises(OSError, f.tell)
|
| 237 |
+
self.assertRaises(OSError, f.seek, 0)
|
| 238 |
+
self.assertRaises(OSError, f.read, 0)
|
| 239 |
+
self.assertRaises(OSError, f.readline, 0)
|
| 240 |
+
|
| 241 |
+
def test_write(self):
|
| 242 |
+
shell = MockShell()
|
| 243 |
+
f = run.StdOutputFile(shell, 'stdout')
|
| 244 |
+
f.write('test')
|
| 245 |
+
self.assertEqual(shell.written, [('test', 'stdout')])
|
| 246 |
+
shell.reset()
|
| 247 |
+
f.write('t\xe8\u015b\U0001d599')
|
| 248 |
+
self.assertEqual(shell.written, [('t\xe8\u015b\U0001d599', 'stdout')])
|
| 249 |
+
shell.reset()
|
| 250 |
+
|
| 251 |
+
f.write(S('t\xe8\u015b\U0001d599'))
|
| 252 |
+
self.assertEqual(shell.written, [('t\xe8\u015b\U0001d599', 'stdout')])
|
| 253 |
+
self.assertEqual(type(shell.written[0][0]), str)
|
| 254 |
+
shell.reset()
|
| 255 |
+
|
| 256 |
+
self.assertRaises(TypeError, f.write)
|
| 257 |
+
self.assertEqual(shell.written, [])
|
| 258 |
+
self.assertRaises(TypeError, f.write, b'test')
|
| 259 |
+
self.assertRaises(TypeError, f.write, 123)
|
| 260 |
+
self.assertEqual(shell.written, [])
|
| 261 |
+
self.assertRaises(TypeError, f.write, 'test', 'spam')
|
| 262 |
+
self.assertEqual(shell.written, [])
|
| 263 |
+
|
| 264 |
+
def test_write_stderr_nonencodable(self):
|
| 265 |
+
shell = MockShell()
|
| 266 |
+
f = run.StdOutputFile(shell, 'stderr', 'iso-8859-15', 'backslashreplace')
|
| 267 |
+
f.write('t\xe8\u015b\U0001d599\xa4')
|
| 268 |
+
self.assertEqual(shell.written, [('t\xe8\\u015b\\U0001d599\\xa4', 'stderr')])
|
| 269 |
+
shell.reset()
|
| 270 |
+
|
| 271 |
+
f.write(S('t\xe8\u015b\U0001d599\xa4'))
|
| 272 |
+
self.assertEqual(shell.written, [('t\xe8\\u015b\\U0001d599\\xa4', 'stderr')])
|
| 273 |
+
self.assertEqual(type(shell.written[0][0]), str)
|
| 274 |
+
shell.reset()
|
| 275 |
+
|
| 276 |
+
self.assertRaises(TypeError, f.write)
|
| 277 |
+
self.assertEqual(shell.written, [])
|
| 278 |
+
self.assertRaises(TypeError, f.write, b'test')
|
| 279 |
+
self.assertRaises(TypeError, f.write, 123)
|
| 280 |
+
self.assertEqual(shell.written, [])
|
| 281 |
+
self.assertRaises(TypeError, f.write, 'test', 'spam')
|
| 282 |
+
self.assertEqual(shell.written, [])
|
| 283 |
+
|
| 284 |
+
def test_writelines(self):
|
| 285 |
+
shell = MockShell()
|
| 286 |
+
f = run.StdOutputFile(shell, 'stdout')
|
| 287 |
+
f.writelines([])
|
| 288 |
+
self.assertEqual(shell.written, [])
|
| 289 |
+
shell.reset()
|
| 290 |
+
f.writelines(['one\n', 'two'])
|
| 291 |
+
self.assertEqual(shell.written,
|
| 292 |
+
[('one\n', 'stdout'), ('two', 'stdout')])
|
| 293 |
+
shell.reset()
|
| 294 |
+
f.writelines(['on\xe8\n', 'tw\xf2'])
|
| 295 |
+
self.assertEqual(shell.written,
|
| 296 |
+
[('on\xe8\n', 'stdout'), ('tw\xf2', 'stdout')])
|
| 297 |
+
shell.reset()
|
| 298 |
+
|
| 299 |
+
f.writelines([S('t\xe8st')])
|
| 300 |
+
self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
|
| 301 |
+
self.assertEqual(type(shell.written[0][0]), str)
|
| 302 |
+
shell.reset()
|
| 303 |
+
|
| 304 |
+
self.assertRaises(TypeError, f.writelines)
|
| 305 |
+
self.assertEqual(shell.written, [])
|
| 306 |
+
self.assertRaises(TypeError, f.writelines, 123)
|
| 307 |
+
self.assertEqual(shell.written, [])
|
| 308 |
+
self.assertRaises(TypeError, f.writelines, [b'test'])
|
| 309 |
+
self.assertRaises(TypeError, f.writelines, [123])
|
| 310 |
+
self.assertEqual(shell.written, [])
|
| 311 |
+
self.assertRaises(TypeError, f.writelines, [], [])
|
| 312 |
+
self.assertEqual(shell.written, [])
|
| 313 |
+
|
| 314 |
+
def test_close(self):
|
| 315 |
+
shell = MockShell()
|
| 316 |
+
f = run.StdOutputFile(shell, 'stdout')
|
| 317 |
+
self.assertFalse(f.closed)
|
| 318 |
+
f.write('test')
|
| 319 |
+
f.close()
|
| 320 |
+
self.assertTrue(f.closed)
|
| 321 |
+
self.assertRaises(ValueError, f.write, 'x')
|
| 322 |
+
self.assertEqual(shell.written, [('test', 'stdout')])
|
| 323 |
+
f.close()
|
| 324 |
+
self.assertRaises(TypeError, f.close, 1)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
class RecursionLimitTest(unittest.TestCase):
|
| 328 |
+
# Test (un)install_recursionlimit_wrappers and fixdoc.
|
| 329 |
+
|
| 330 |
+
def test_bad_setrecursionlimit_calls(self):
|
| 331 |
+
run.install_recursionlimit_wrappers()
|
| 332 |
+
self.addCleanup(run.uninstall_recursionlimit_wrappers)
|
| 333 |
+
f = sys.setrecursionlimit
|
| 334 |
+
self.assertRaises(TypeError, f, limit=100)
|
| 335 |
+
self.assertRaises(TypeError, f, 100, 1000)
|
| 336 |
+
self.assertRaises(ValueError, f, 0)
|
| 337 |
+
|
| 338 |
+
def test_roundtrip(self):
|
| 339 |
+
run.install_recursionlimit_wrappers()
|
| 340 |
+
self.addCleanup(run.uninstall_recursionlimit_wrappers)
|
| 341 |
+
|
| 342 |
+
# Check that setting the recursion limit works.
|
| 343 |
+
orig_reclimit = sys.getrecursionlimit()
|
| 344 |
+
self.addCleanup(sys.setrecursionlimit, orig_reclimit)
|
| 345 |
+
sys.setrecursionlimit(orig_reclimit + 3)
|
| 346 |
+
|
| 347 |
+
# Check that the new limit is returned by sys.getrecursionlimit().
|
| 348 |
+
new_reclimit = sys.getrecursionlimit()
|
| 349 |
+
self.assertEqual(new_reclimit, orig_reclimit + 3)
|
| 350 |
+
|
| 351 |
+
def test_default_recursion_limit_preserved(self):
|
| 352 |
+
orig_reclimit = sys.getrecursionlimit()
|
| 353 |
+
run.install_recursionlimit_wrappers()
|
| 354 |
+
self.addCleanup(run.uninstall_recursionlimit_wrappers)
|
| 355 |
+
new_reclimit = sys.getrecursionlimit()
|
| 356 |
+
self.assertEqual(new_reclimit, orig_reclimit)
|
| 357 |
+
|
| 358 |
+
def test_fixdoc(self):
|
| 359 |
+
# Put here until better place for miscellaneous test.
|
| 360 |
+
def func(): "docstring"
|
| 361 |
+
run.fixdoc(func, "more")
|
| 362 |
+
self.assertEqual(func.__doc__, "docstring\n\nmore")
|
| 363 |
+
func.__doc__ = None
|
| 364 |
+
run.fixdoc(func, "more")
|
| 365 |
+
self.assertEqual(func.__doc__, "more")
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
class HandleErrorTest(unittest.TestCase):
|
| 369 |
+
# Method of MyRPCServer
|
| 370 |
+
def test_fatal_error(self):
|
| 371 |
+
eq = self.assertEqual
|
| 372 |
+
with captured_output('__stderr__') as err,\
|
| 373 |
+
mock.patch('idlelib.run.thread.interrupt_main',
|
| 374 |
+
new_callable=Func) as func:
|
| 375 |
+
try:
|
| 376 |
+
raise EOFError
|
| 377 |
+
except EOFError:
|
| 378 |
+
run.MyRPCServer.handle_error(None, 'abc', '123')
|
| 379 |
+
eq(run.exit_now, True)
|
| 380 |
+
run.exit_now = False
|
| 381 |
+
eq(err.getvalue(), '')
|
| 382 |
+
|
| 383 |
+
try:
|
| 384 |
+
raise IndexError
|
| 385 |
+
except IndexError:
|
| 386 |
+
run.MyRPCServer.handle_error(None, 'abc', '123')
|
| 387 |
+
eq(run.quitting, True)
|
| 388 |
+
run.quitting = False
|
| 389 |
+
msg = err.getvalue()
|
| 390 |
+
self.assertIn('abc', msg)
|
| 391 |
+
self.assertIn('123', msg)
|
| 392 |
+
self.assertIn('IndexError', msg)
|
| 393 |
+
eq(func.called, 2)
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
class ExecRuncodeTest(unittest.TestCase):
|
| 397 |
+
|
| 398 |
+
@classmethod
|
| 399 |
+
def setUpClass(cls):
|
| 400 |
+
cls.addClassCleanup(setattr,run,'print_exception',run.print_exception)
|
| 401 |
+
cls.prt = Func() # Need reference.
|
| 402 |
+
run.print_exception = cls.prt
|
| 403 |
+
mockrpc = mock.Mock()
|
| 404 |
+
mockrpc.console.getvar = Func(result=False)
|
| 405 |
+
cls.ex = run.Executive(mockrpc)
|
| 406 |
+
|
| 407 |
+
@classmethod
|
| 408 |
+
def tearDownClass(cls):
|
| 409 |
+
assert sys.excepthook == sys.__excepthook__
|
| 410 |
+
|
| 411 |
+
def test_exceptions(self):
|
| 412 |
+
ex = self.ex
|
| 413 |
+
ex.runcode('1/0')
|
| 414 |
+
self.assertIs(ex.user_exc_info[0], ZeroDivisionError)
|
| 415 |
+
|
| 416 |
+
self.addCleanup(setattr, sys, 'excepthook', sys.__excepthook__)
|
| 417 |
+
sys.excepthook = lambda t, e, tb: run.print_exception(t)
|
| 418 |
+
ex.runcode('1/0')
|
| 419 |
+
self.assertIs(self.prt.args[0], ZeroDivisionError)
|
| 420 |
+
|
| 421 |
+
sys.excepthook = lambda: None
|
| 422 |
+
ex.runcode('1/0')
|
| 423 |
+
t, e, tb = ex.user_exc_info
|
| 424 |
+
self.assertIs(t, TypeError)
|
| 425 |
+
self.assertTrue(isinstance(e.__context__, ZeroDivisionError))
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
if __name__ == '__main__':
|
| 429 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_searchbase.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ittest
|
| 6 |
+
from test.support import requires
|
| 7 |
+
from tkinter import Text, Tk, Toplevel
|
| 8 |
+
from tkinter.ttk import Frame
|
| 9 |
+
from idlelib import searchengine as se
|
| 10 |
+
from idlelib import searchbase as sdb
|
| 11 |
+
from idlelib.idle_test.mock_idle import Func
|
| 12 |
+
## from idlelib.idle_test.mock_tk import Var
|
| 13 |
+
|
| 14 |
+
# The ## imports above & following could help make some tests gui-free.
|
| 15 |
+
# However, they currently make radiobutton tests fail.
|
| 16 |
+
##def setUpModule():
|
| 17 |
+
## # Replace tk objects used to initialize se.SearchEngine.
|
| 18 |
+
## se.BooleanVar = Var
|
| 19 |
+
## se.StringVar = Var
|
| 20 |
+
##
|
| 21 |
+
##def tearDownModule():
|
| 22 |
+
## se.BooleanVar = BooleanVar
|
| 23 |
+
## se.StringVar = StringVar
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class SearchDialogBaseTest(unittest.TestCase):
|
| 27 |
+
|
| 28 |
+
@classmethod
|
| 29 |
+
def setUpClass(cls):
|
| 30 |
+
requires('gui')
|
| 31 |
+
cls.root = Tk()
|
| 32 |
+
|
| 33 |
+
@classmethod
|
| 34 |
+
def tearDownClass(cls):
|
| 35 |
+
cls.root.update_idletasks()
|
| 36 |
+
cls.root.destroy()
|
| 37 |
+
del cls.root
|
| 38 |
+
|
| 39 |
+
def setUp(self):
|
| 40 |
+
self.engine = se.SearchEngine(self.root) # None also seems to work
|
| 41 |
+
self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)
|
| 42 |
+
|
| 43 |
+
def tearDown(self):
|
| 44 |
+
self.dialog.close()
|
| 45 |
+
|
| 46 |
+
def test_open_and_close(self):
|
| 47 |
+
# open calls create_widgets, which needs default_command
|
| 48 |
+
self.dialog.default_command = None
|
| 49 |
+
|
| 50 |
+
toplevel = Toplevel(self.root)
|
| 51 |
+
text = Text(toplevel)
|
| 52 |
+
self.dialog.open(text)
|
| 53 |
+
self.assertEqual(self.dialog.top.state(), 'normal')
|
| 54 |
+
self.dialog.close()
|
| 55 |
+
self.assertEqual(self.dialog.top.state(), 'withdrawn')
|
| 56 |
+
|
| 57 |
+
self.dialog.open(text, searchphrase="hello")
|
| 58 |
+
self.assertEqual(self.dialog.ent.get(), 'hello')
|
| 59 |
+
toplevel.update_idletasks()
|
| 60 |
+
toplevel.destroy()
|
| 61 |
+
|
| 62 |
+
def test_create_widgets(self):
|
| 63 |
+
self.dialog.create_entries = Func()
|
| 64 |
+
self.dialog.create_option_buttons = Func()
|
| 65 |
+
self.dialog.create_other_buttons = Func()
|
| 66 |
+
self.dialog.create_command_buttons = Func()
|
| 67 |
+
|
| 68 |
+
self.dialog.default_command = None
|
| 69 |
+
self.dialog.create_widgets()
|
| 70 |
+
|
| 71 |
+
self.assertTrue(self.dialog.create_entries.called)
|
| 72 |
+
self.assertTrue(self.dialog.create_option_buttons.called)
|
| 73 |
+
self.assertTrue(self.dialog.create_other_buttons.called)
|
| 74 |
+
self.assertTrue(self.dialog.create_command_buttons.called)
|
| 75 |
+
|
| 76 |
+
def test_make_entry(self):
|
| 77 |
+
equal = self.assertEqual
|
| 78 |
+
self.dialog.row = 0
|
| 79 |
+
self.dialog.frame = Frame(self.root)
|
| 80 |
+
entry, label = self.dialog.make_entry("Test:", 'hello')
|
| 81 |
+
equal(label['text'], 'Test:')
|
| 82 |
+
|
| 83 |
+
self.assertIn(entry.get(), 'hello')
|
| 84 |
+
egi = entry.grid_info()
|
| 85 |
+
equal(int(egi['row']), 0)
|
| 86 |
+
equal(int(egi['column']), 1)
|
| 87 |
+
equal(int(egi['rowspan']), 1)
|
| 88 |
+
equal(int(egi['columnspan']), 1)
|
| 89 |
+
equal(self.dialog.row, 1)
|
| 90 |
+
|
| 91 |
+
def test_create_entries(self):
|
| 92 |
+
self.dialog.frame = Frame(self.root)
|
| 93 |
+
self.dialog.row = 0
|
| 94 |
+
self.engine.setpat('hello')
|
| 95 |
+
self.dialog.create_entries()
|
| 96 |
+
self.assertIn(self.dialog.ent.get(), 'hello')
|
| 97 |
+
|
| 98 |
+
def test_make_frame(self):
|
| 99 |
+
self.dialog.row = 0
|
| 100 |
+
self.dialog.frame = Frame(self.root)
|
| 101 |
+
frame, label = self.dialog.make_frame()
|
| 102 |
+
self.assertEqual(label, '')
|
| 103 |
+
self.assertEqual(str(type(frame)), "<class 'tkinter.ttk.Frame'>")
|
| 104 |
+
# self.assertIsInstance(frame, Frame) fails when test is run by
|
| 105 |
+
# test_idle not run from IDLE editor. See issue 33987 PR.
|
| 106 |
+
|
| 107 |
+
frame, label = self.dialog.make_frame('testlabel')
|
| 108 |
+
self.assertEqual(label['text'], 'testlabel')
|
| 109 |
+
|
| 110 |
+
def btn_test_setup(self, meth):
|
| 111 |
+
self.dialog.frame = Frame(self.root)
|
| 112 |
+
self.dialog.row = 0
|
| 113 |
+
return meth()
|
| 114 |
+
|
| 115 |
+
def test_create_option_buttons(self):
|
| 116 |
+
e = self.engine
|
| 117 |
+
for state in (0, 1):
|
| 118 |
+
for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
|
| 119 |
+
var.set(state)
|
| 120 |
+
frame, options = self.btn_test_setup(
|
| 121 |
+
self.dialog.create_option_buttons)
|
| 122 |
+
for spec, button in zip (options, frame.pack_slaves()):
|
| 123 |
+
var, label = spec
|
| 124 |
+
self.assertEqual(button['text'], label)
|
| 125 |
+
self.assertEqual(var.get(), state)
|
| 126 |
+
|
| 127 |
+
def test_create_other_buttons(self):
|
| 128 |
+
for state in (False, True):
|
| 129 |
+
var = self.engine.backvar
|
| 130 |
+
var.set(state)
|
| 131 |
+
frame, others = self.btn_test_setup(
|
| 132 |
+
self.dialog.create_other_buttons)
|
| 133 |
+
buttons = frame.pack_slaves()
|
| 134 |
+
for spec, button in zip(others, buttons):
|
| 135 |
+
val, label = spec
|
| 136 |
+
self.assertEqual(button['text'], label)
|
| 137 |
+
if val == state:
|
| 138 |
+
# hit other button, then this one
|
| 139 |
+
# indexes depend on button order
|
| 140 |
+
self.assertEqual(var.get(), state)
|
| 141 |
+
|
| 142 |
+
def test_make_button(self):
|
| 143 |
+
self.dialog.frame = Frame(self.root)
|
| 144 |
+
self.dialog.buttonframe = Frame(self.dialog.frame)
|
| 145 |
+
btn = self.dialog.make_button('Test', self.dialog.close)
|
| 146 |
+
self.assertEqual(btn['text'], 'Test')
|
| 147 |
+
|
| 148 |
+
def test_create_command_buttons(self):
|
| 149 |
+
self.dialog.frame = Frame(self.root)
|
| 150 |
+
self.dialog.create_command_buttons()
|
| 151 |
+
# Look for close button command in buttonframe
|
| 152 |
+
closebuttoncommand = ''
|
| 153 |
+
for child in self.dialog.buttonframe.winfo_children():
|
| 154 |
+
if child['text'] == 'Close':
|
| 155 |
+
closebuttoncommand = child['command']
|
| 156 |
+
self.assertIn('close', closebuttoncommand)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if __name__ == '__main__':
|
| 160 |
+
unittest.main(verbosity=2, exit=2)
|
llava/lib/python3.10/idlelib/idle_test/test_sidebar.py
ADDED
|
@@ -0,0 +1,768 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test sidebar, coverage 85%"""
|
| 2 |
+
from textwrap import dedent
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
from itertools import chain
|
| 6 |
+
import unittest
|
| 7 |
+
import unittest.mock
|
| 8 |
+
from test.support import requires, swap_attr
|
| 9 |
+
from test import support
|
| 10 |
+
import tkinter as tk
|
| 11 |
+
from idlelib.idle_test.tkinter_testing_utils import run_in_tk_mainloop
|
| 12 |
+
|
| 13 |
+
from idlelib.delegator import Delegator
|
| 14 |
+
from idlelib.editor import fixwordbreaks
|
| 15 |
+
from idlelib.percolator import Percolator
|
| 16 |
+
import idlelib.pyshell
|
| 17 |
+
from idlelib.pyshell import fix_x11_paste, PyShell, PyShellFileList
|
| 18 |
+
from idlelib.run import fix_scaling
|
| 19 |
+
import idlelib.sidebar
|
| 20 |
+
from idlelib.sidebar import get_end_linenumber, get_lineno
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Dummy_editwin:
|
| 24 |
+
def __init__(self, text):
|
| 25 |
+
self.text = text
|
| 26 |
+
self.text_frame = self.text.master
|
| 27 |
+
self.per = Percolator(text)
|
| 28 |
+
self.undo = Delegator()
|
| 29 |
+
self.per.insertfilter(self.undo)
|
| 30 |
+
|
| 31 |
+
def setvar(self, name, value):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
def getlineno(self, index):
|
| 35 |
+
return int(float(self.text.index(index)))
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class LineNumbersTest(unittest.TestCase):
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def setUpClass(cls):
|
| 42 |
+
requires('gui')
|
| 43 |
+
cls.root = tk.Tk()
|
| 44 |
+
cls.root.withdraw()
|
| 45 |
+
|
| 46 |
+
cls.text_frame = tk.Frame(cls.root)
|
| 47 |
+
cls.text_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
|
| 48 |
+
cls.text_frame.rowconfigure(1, weight=1)
|
| 49 |
+
cls.text_frame.columnconfigure(1, weight=1)
|
| 50 |
+
|
| 51 |
+
cls.text = tk.Text(cls.text_frame, width=80, height=24, wrap=tk.NONE)
|
| 52 |
+
cls.text.grid(row=1, column=1, sticky=tk.NSEW)
|
| 53 |
+
|
| 54 |
+
cls.editwin = Dummy_editwin(cls.text)
|
| 55 |
+
cls.editwin.vbar = tk.Scrollbar(cls.text_frame)
|
| 56 |
+
|
| 57 |
+
@classmethod
|
| 58 |
+
def tearDownClass(cls):
|
| 59 |
+
cls.editwin.per.close()
|
| 60 |
+
cls.root.update()
|
| 61 |
+
cls.root.destroy()
|
| 62 |
+
del cls.text, cls.text_frame, cls.editwin, cls.root
|
| 63 |
+
|
| 64 |
+
def setUp(self):
|
| 65 |
+
self.linenumber = idlelib.sidebar.LineNumbers(self.editwin)
|
| 66 |
+
|
| 67 |
+
self.highlight_cfg = {"background": '#abcdef',
|
| 68 |
+
"foreground": '#123456'}
|
| 69 |
+
orig_idleConf_GetHighlight = idlelib.sidebar.idleConf.GetHighlight
|
| 70 |
+
def mock_idleconf_GetHighlight(theme, element):
|
| 71 |
+
if element == 'linenumber':
|
| 72 |
+
return self.highlight_cfg
|
| 73 |
+
return orig_idleConf_GetHighlight(theme, element)
|
| 74 |
+
GetHighlight_patcher = unittest.mock.patch.object(
|
| 75 |
+
idlelib.sidebar.idleConf, 'GetHighlight', mock_idleconf_GetHighlight)
|
| 76 |
+
GetHighlight_patcher.start()
|
| 77 |
+
self.addCleanup(GetHighlight_patcher.stop)
|
| 78 |
+
|
| 79 |
+
self.font_override = 'TkFixedFont'
|
| 80 |
+
def mock_idleconf_GetFont(root, configType, section):
|
| 81 |
+
return self.font_override
|
| 82 |
+
GetFont_patcher = unittest.mock.patch.object(
|
| 83 |
+
idlelib.sidebar.idleConf, 'GetFont', mock_idleconf_GetFont)
|
| 84 |
+
GetFont_patcher.start()
|
| 85 |
+
self.addCleanup(GetFont_patcher.stop)
|
| 86 |
+
|
| 87 |
+
def tearDown(self):
|
| 88 |
+
self.text.delete('1.0', 'end')
|
| 89 |
+
|
| 90 |
+
def get_selection(self):
|
| 91 |
+
return tuple(map(str, self.text.tag_ranges('sel')))
|
| 92 |
+
|
| 93 |
+
def get_line_screen_position(self, line):
|
| 94 |
+
bbox = self.linenumber.sidebar_text.bbox(f'{line}.end -1c')
|
| 95 |
+
x = bbox[0] + 2
|
| 96 |
+
y = bbox[1] + 2
|
| 97 |
+
return x, y
|
| 98 |
+
|
| 99 |
+
def assert_state_disabled(self):
|
| 100 |
+
state = self.linenumber.sidebar_text.config()['state']
|
| 101 |
+
self.assertEqual(state[-1], tk.DISABLED)
|
| 102 |
+
|
| 103 |
+
def get_sidebar_text_contents(self):
|
| 104 |
+
return self.linenumber.sidebar_text.get('1.0', tk.END)
|
| 105 |
+
|
| 106 |
+
def assert_sidebar_n_lines(self, n_lines):
|
| 107 |
+
expected = '\n'.join(chain(map(str, range(1, n_lines + 1)), ['']))
|
| 108 |
+
self.assertEqual(self.get_sidebar_text_contents(), expected)
|
| 109 |
+
|
| 110 |
+
def assert_text_equals(self, expected):
|
| 111 |
+
return self.assertEqual(self.text.get('1.0', 'end'), expected)
|
| 112 |
+
|
| 113 |
+
def test_init_empty(self):
|
| 114 |
+
self.assert_sidebar_n_lines(1)
|
| 115 |
+
|
| 116 |
+
def test_init_not_empty(self):
|
| 117 |
+
self.text.insert('insert', 'foo bar\n'*3)
|
| 118 |
+
self.assert_text_equals('foo bar\n'*3 + '\n')
|
| 119 |
+
self.assert_sidebar_n_lines(4)
|
| 120 |
+
|
| 121 |
+
def test_toggle_linenumbering(self):
|
| 122 |
+
self.assertEqual(self.linenumber.is_shown, False)
|
| 123 |
+
self.linenumber.show_sidebar()
|
| 124 |
+
self.assertEqual(self.linenumber.is_shown, True)
|
| 125 |
+
self.linenumber.hide_sidebar()
|
| 126 |
+
self.assertEqual(self.linenumber.is_shown, False)
|
| 127 |
+
self.linenumber.hide_sidebar()
|
| 128 |
+
self.assertEqual(self.linenumber.is_shown, False)
|
| 129 |
+
self.linenumber.show_sidebar()
|
| 130 |
+
self.assertEqual(self.linenumber.is_shown, True)
|
| 131 |
+
self.linenumber.show_sidebar()
|
| 132 |
+
self.assertEqual(self.linenumber.is_shown, True)
|
| 133 |
+
|
| 134 |
+
def test_insert(self):
|
| 135 |
+
self.text.insert('insert', 'foobar')
|
| 136 |
+
self.assert_text_equals('foobar\n')
|
| 137 |
+
self.assert_sidebar_n_lines(1)
|
| 138 |
+
self.assert_state_disabled()
|
| 139 |
+
|
| 140 |
+
self.text.insert('insert', '\nfoo')
|
| 141 |
+
self.assert_text_equals('foobar\nfoo\n')
|
| 142 |
+
self.assert_sidebar_n_lines(2)
|
| 143 |
+
self.assert_state_disabled()
|
| 144 |
+
|
| 145 |
+
self.text.insert('insert', 'hello\n'*2)
|
| 146 |
+
self.assert_text_equals('foobar\nfoohello\nhello\n\n')
|
| 147 |
+
self.assert_sidebar_n_lines(4)
|
| 148 |
+
self.assert_state_disabled()
|
| 149 |
+
|
| 150 |
+
self.text.insert('insert', '\nworld')
|
| 151 |
+
self.assert_text_equals('foobar\nfoohello\nhello\n\nworld\n')
|
| 152 |
+
self.assert_sidebar_n_lines(5)
|
| 153 |
+
self.assert_state_disabled()
|
| 154 |
+
|
| 155 |
+
def test_delete(self):
|
| 156 |
+
self.text.insert('insert', 'foobar')
|
| 157 |
+
self.assert_text_equals('foobar\n')
|
| 158 |
+
self.text.delete('1.1', '1.3')
|
| 159 |
+
self.assert_text_equals('fbar\n')
|
| 160 |
+
self.assert_sidebar_n_lines(1)
|
| 161 |
+
self.assert_state_disabled()
|
| 162 |
+
|
| 163 |
+
self.text.insert('insert', 'foo\n'*2)
|
| 164 |
+
self.assert_text_equals('fbarfoo\nfoo\n\n')
|
| 165 |
+
self.assert_sidebar_n_lines(3)
|
| 166 |
+
self.assert_state_disabled()
|
| 167 |
+
|
| 168 |
+
# Deleting up to "2.end" doesn't delete the final newline.
|
| 169 |
+
self.text.delete('2.0', '2.end')
|
| 170 |
+
self.assert_text_equals('fbarfoo\n\n\n')
|
| 171 |
+
self.assert_sidebar_n_lines(3)
|
| 172 |
+
self.assert_state_disabled()
|
| 173 |
+
|
| 174 |
+
self.text.delete('1.3', 'end')
|
| 175 |
+
self.assert_text_equals('fba\n')
|
| 176 |
+
self.assert_sidebar_n_lines(1)
|
| 177 |
+
self.assert_state_disabled()
|
| 178 |
+
|
| 179 |
+
# Text widgets always keep a single '\n' character at the end.
|
| 180 |
+
self.text.delete('1.0', 'end')
|
| 181 |
+
self.assert_text_equals('\n')
|
| 182 |
+
self.assert_sidebar_n_lines(1)
|
| 183 |
+
self.assert_state_disabled()
|
| 184 |
+
|
| 185 |
+
def test_sidebar_text_width(self):
|
| 186 |
+
"""
|
| 187 |
+
Test that linenumber text widget is always at the minimum
|
| 188 |
+
width
|
| 189 |
+
"""
|
| 190 |
+
def get_width():
|
| 191 |
+
return self.linenumber.sidebar_text.config()['width'][-1]
|
| 192 |
+
|
| 193 |
+
self.assert_sidebar_n_lines(1)
|
| 194 |
+
self.assertEqual(get_width(), 1)
|
| 195 |
+
|
| 196 |
+
self.text.insert('insert', 'foo')
|
| 197 |
+
self.assert_sidebar_n_lines(1)
|
| 198 |
+
self.assertEqual(get_width(), 1)
|
| 199 |
+
|
| 200 |
+
self.text.insert('insert', 'foo\n'*8)
|
| 201 |
+
self.assert_sidebar_n_lines(9)
|
| 202 |
+
self.assertEqual(get_width(), 1)
|
| 203 |
+
|
| 204 |
+
self.text.insert('insert', 'foo\n')
|
| 205 |
+
self.assert_sidebar_n_lines(10)
|
| 206 |
+
self.assertEqual(get_width(), 2)
|
| 207 |
+
|
| 208 |
+
self.text.insert('insert', 'foo\n')
|
| 209 |
+
self.assert_sidebar_n_lines(11)
|
| 210 |
+
self.assertEqual(get_width(), 2)
|
| 211 |
+
|
| 212 |
+
self.text.delete('insert -1l linestart', 'insert linestart')
|
| 213 |
+
self.assert_sidebar_n_lines(10)
|
| 214 |
+
self.assertEqual(get_width(), 2)
|
| 215 |
+
|
| 216 |
+
self.text.delete('insert -1l linestart', 'insert linestart')
|
| 217 |
+
self.assert_sidebar_n_lines(9)
|
| 218 |
+
self.assertEqual(get_width(), 1)
|
| 219 |
+
|
| 220 |
+
self.text.insert('insert', 'foo\n'*90)
|
| 221 |
+
self.assert_sidebar_n_lines(99)
|
| 222 |
+
self.assertEqual(get_width(), 2)
|
| 223 |
+
|
| 224 |
+
self.text.insert('insert', 'foo\n')
|
| 225 |
+
self.assert_sidebar_n_lines(100)
|
| 226 |
+
self.assertEqual(get_width(), 3)
|
| 227 |
+
|
| 228 |
+
self.text.insert('insert', 'foo\n')
|
| 229 |
+
self.assert_sidebar_n_lines(101)
|
| 230 |
+
self.assertEqual(get_width(), 3)
|
| 231 |
+
|
| 232 |
+
self.text.delete('insert -1l linestart', 'insert linestart')
|
| 233 |
+
self.assert_sidebar_n_lines(100)
|
| 234 |
+
self.assertEqual(get_width(), 3)
|
| 235 |
+
|
| 236 |
+
self.text.delete('insert -1l linestart', 'insert linestart')
|
| 237 |
+
self.assert_sidebar_n_lines(99)
|
| 238 |
+
self.assertEqual(get_width(), 2)
|
| 239 |
+
|
| 240 |
+
self.text.delete('50.0 -1c', 'end -1c')
|
| 241 |
+
self.assert_sidebar_n_lines(49)
|
| 242 |
+
self.assertEqual(get_width(), 2)
|
| 243 |
+
|
| 244 |
+
self.text.delete('5.0 -1c', 'end -1c')
|
| 245 |
+
self.assert_sidebar_n_lines(4)
|
| 246 |
+
self.assertEqual(get_width(), 1)
|
| 247 |
+
|
| 248 |
+
# Text widgets always keep a single '\n' character at the end.
|
| 249 |
+
self.text.delete('1.0', 'end -1c')
|
| 250 |
+
self.assert_sidebar_n_lines(1)
|
| 251 |
+
self.assertEqual(get_width(), 1)
|
| 252 |
+
|
| 253 |
+
# The following tests are temporarily disabled due to relying on
|
| 254 |
+
# simulated user input and inspecting which text is selected, which
|
| 255 |
+
# are fragile and can fail when several GUI tests are run in parallel
|
| 256 |
+
# or when the windows created by the test lose focus.
|
| 257 |
+
#
|
| 258 |
+
# TODO: Re-work these tests or remove them from the test suite.
|
| 259 |
+
|
| 260 |
+
@unittest.skip('test disabled')
|
| 261 |
+
def test_click_selection(self):
|
| 262 |
+
self.linenumber.show_sidebar()
|
| 263 |
+
self.text.insert('1.0', 'one\ntwo\nthree\nfour\n')
|
| 264 |
+
self.root.update()
|
| 265 |
+
|
| 266 |
+
# Click on the second line.
|
| 267 |
+
x, y = self.get_line_screen_position(2)
|
| 268 |
+
self.linenumber.sidebar_text.event_generate('<Button-1>', x=x, y=y)
|
| 269 |
+
self.linenumber.sidebar_text.update()
|
| 270 |
+
self.root.update()
|
| 271 |
+
|
| 272 |
+
self.assertEqual(self.get_selection(), ('2.0', '3.0'))
|
| 273 |
+
|
| 274 |
+
def simulate_drag(self, start_line, end_line):
|
| 275 |
+
start_x, start_y = self.get_line_screen_position(start_line)
|
| 276 |
+
end_x, end_y = self.get_line_screen_position(end_line)
|
| 277 |
+
|
| 278 |
+
self.linenumber.sidebar_text.event_generate('<Button-1>',
|
| 279 |
+
x=start_x, y=start_y)
|
| 280 |
+
self.root.update()
|
| 281 |
+
|
| 282 |
+
def lerp(a, b, steps):
|
| 283 |
+
"""linearly interpolate from a to b (inclusive) in equal steps"""
|
| 284 |
+
last_step = steps - 1
|
| 285 |
+
for i in range(steps):
|
| 286 |
+
yield ((last_step - i) / last_step) * a + (i / last_step) * b
|
| 287 |
+
|
| 288 |
+
for x, y in zip(
|
| 289 |
+
map(int, lerp(start_x, end_x, steps=11)),
|
| 290 |
+
map(int, lerp(start_y, end_y, steps=11)),
|
| 291 |
+
):
|
| 292 |
+
self.linenumber.sidebar_text.event_generate('<B1-Motion>', x=x, y=y)
|
| 293 |
+
self.root.update()
|
| 294 |
+
|
| 295 |
+
self.linenumber.sidebar_text.event_generate('<ButtonRelease-1>',
|
| 296 |
+
x=end_x, y=end_y)
|
| 297 |
+
self.root.update()
|
| 298 |
+
|
| 299 |
+
@unittest.skip('test disabled')
|
| 300 |
+
def test_drag_selection_down(self):
|
| 301 |
+
self.linenumber.show_sidebar()
|
| 302 |
+
self.text.insert('1.0', 'one\ntwo\nthree\nfour\nfive\n')
|
| 303 |
+
self.root.update()
|
| 304 |
+
|
| 305 |
+
# Drag from the second line to the fourth line.
|
| 306 |
+
self.simulate_drag(2, 4)
|
| 307 |
+
self.assertEqual(self.get_selection(), ('2.0', '5.0'))
|
| 308 |
+
|
| 309 |
+
@unittest.skip('test disabled')
|
| 310 |
+
def test_drag_selection_up(self):
|
| 311 |
+
self.linenumber.show_sidebar()
|
| 312 |
+
self.text.insert('1.0', 'one\ntwo\nthree\nfour\nfive\n')
|
| 313 |
+
self.root.update()
|
| 314 |
+
|
| 315 |
+
# Drag from the fourth line to the second line.
|
| 316 |
+
self.simulate_drag(4, 2)
|
| 317 |
+
self.assertEqual(self.get_selection(), ('2.0', '5.0'))
|
| 318 |
+
|
| 319 |
+
def test_scroll(self):
|
| 320 |
+
self.linenumber.show_sidebar()
|
| 321 |
+
self.text.insert('1.0', 'line\n' * 100)
|
| 322 |
+
self.root.update()
|
| 323 |
+
|
| 324 |
+
# Scroll down 10 lines.
|
| 325 |
+
self.text.yview_scroll(10, 'unit')
|
| 326 |
+
self.root.update()
|
| 327 |
+
self.assertEqual(self.text.index('@0,0'), '11.0')
|
| 328 |
+
self.assertEqual(self.linenumber.sidebar_text.index('@0,0'), '11.0')
|
| 329 |
+
|
| 330 |
+
# Generate a mouse-wheel event and make sure it scrolled up or down.
|
| 331 |
+
# The meaning of the "delta" is OS-dependant, so this just checks for
|
| 332 |
+
# any change.
|
| 333 |
+
self.linenumber.sidebar_text.event_generate('<MouseWheel>',
|
| 334 |
+
x=0, y=0,
|
| 335 |
+
delta=10)
|
| 336 |
+
self.root.update()
|
| 337 |
+
self.assertNotEqual(self.text.index('@0,0'), '11.0')
|
| 338 |
+
self.assertNotEqual(self.linenumber.sidebar_text.index('@0,0'), '11.0')
|
| 339 |
+
|
| 340 |
+
def test_font(self):
|
| 341 |
+
ln = self.linenumber
|
| 342 |
+
|
| 343 |
+
orig_font = ln.sidebar_text['font']
|
| 344 |
+
test_font = 'TkTextFont'
|
| 345 |
+
self.assertNotEqual(orig_font, test_font)
|
| 346 |
+
|
| 347 |
+
# Ensure line numbers aren't shown.
|
| 348 |
+
ln.hide_sidebar()
|
| 349 |
+
|
| 350 |
+
self.font_override = test_font
|
| 351 |
+
# Nothing breaks when line numbers aren't shown.
|
| 352 |
+
ln.update_font()
|
| 353 |
+
|
| 354 |
+
# Activate line numbers, previous font change is immediately effective.
|
| 355 |
+
ln.show_sidebar()
|
| 356 |
+
self.assertEqual(ln.sidebar_text['font'], test_font)
|
| 357 |
+
|
| 358 |
+
# Call the font update with line numbers shown, change is picked up.
|
| 359 |
+
self.font_override = orig_font
|
| 360 |
+
ln.update_font()
|
| 361 |
+
self.assertEqual(ln.sidebar_text['font'], orig_font)
|
| 362 |
+
|
| 363 |
+
def test_highlight_colors(self):
|
| 364 |
+
ln = self.linenumber
|
| 365 |
+
|
| 366 |
+
orig_colors = dict(self.highlight_cfg)
|
| 367 |
+
test_colors = {'background': '#222222', 'foreground': '#ffff00'}
|
| 368 |
+
|
| 369 |
+
def assert_colors_are_equal(colors):
|
| 370 |
+
self.assertEqual(ln.sidebar_text['background'], colors['background'])
|
| 371 |
+
self.assertEqual(ln.sidebar_text['foreground'], colors['foreground'])
|
| 372 |
+
|
| 373 |
+
# Ensure line numbers aren't shown.
|
| 374 |
+
ln.hide_sidebar()
|
| 375 |
+
|
| 376 |
+
self.highlight_cfg = test_colors
|
| 377 |
+
# Nothing breaks with inactive line numbers.
|
| 378 |
+
ln.update_colors()
|
| 379 |
+
|
| 380 |
+
# Show line numbers, previous colors change is immediately effective.
|
| 381 |
+
ln.show_sidebar()
|
| 382 |
+
assert_colors_are_equal(test_colors)
|
| 383 |
+
|
| 384 |
+
# Call colors update with no change to the configured colors.
|
| 385 |
+
ln.update_colors()
|
| 386 |
+
assert_colors_are_equal(test_colors)
|
| 387 |
+
|
| 388 |
+
# Call the colors update with line numbers shown, change is picked up.
|
| 389 |
+
self.highlight_cfg = orig_colors
|
| 390 |
+
ln.update_colors()
|
| 391 |
+
assert_colors_are_equal(orig_colors)
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
class ShellSidebarTest(unittest.TestCase):
|
| 395 |
+
root: tk.Tk = None
|
| 396 |
+
shell: PyShell = None
|
| 397 |
+
|
| 398 |
+
@classmethod
|
| 399 |
+
def setUpClass(cls):
|
| 400 |
+
requires('gui')
|
| 401 |
+
|
| 402 |
+
cls.root = root = tk.Tk()
|
| 403 |
+
root.withdraw()
|
| 404 |
+
|
| 405 |
+
fix_scaling(root)
|
| 406 |
+
fixwordbreaks(root)
|
| 407 |
+
fix_x11_paste(root)
|
| 408 |
+
|
| 409 |
+
cls.flist = flist = PyShellFileList(root)
|
| 410 |
+
# See #43981 about macosx.setupApp(root, flist) causing failure.
|
| 411 |
+
root.update_idletasks()
|
| 412 |
+
|
| 413 |
+
cls.init_shell()
|
| 414 |
+
|
| 415 |
+
@classmethod
|
| 416 |
+
def tearDownClass(cls):
|
| 417 |
+
if cls.shell is not None:
|
| 418 |
+
cls.shell.executing = False
|
| 419 |
+
cls.shell.close()
|
| 420 |
+
cls.shell = None
|
| 421 |
+
cls.flist = None
|
| 422 |
+
cls.root.update_idletasks()
|
| 423 |
+
cls.root.destroy()
|
| 424 |
+
cls.root = None
|
| 425 |
+
|
| 426 |
+
@classmethod
|
| 427 |
+
def init_shell(cls):
|
| 428 |
+
cls.shell = cls.flist.open_shell()
|
| 429 |
+
cls.shell.pollinterval = 10
|
| 430 |
+
cls.root.update()
|
| 431 |
+
cls.n_preface_lines = get_lineno(cls.shell.text, 'end-1c') - 1
|
| 432 |
+
|
| 433 |
+
@classmethod
|
| 434 |
+
def reset_shell(cls):
|
| 435 |
+
cls.shell.per.bottom.delete(f'{cls.n_preface_lines+1}.0', 'end-1c')
|
| 436 |
+
cls.shell.shell_sidebar.update_sidebar()
|
| 437 |
+
cls.root.update()
|
| 438 |
+
|
| 439 |
+
def setUp(self):
|
| 440 |
+
# In some test environments, e.g. Azure Pipelines (as of
|
| 441 |
+
# Apr. 2021), sys.stdout is changed between tests. However,
|
| 442 |
+
# PyShell relies on overriding sys.stdout when run without a
|
| 443 |
+
# sub-process (as done here; see setUpClass).
|
| 444 |
+
self._saved_stdout = None
|
| 445 |
+
if sys.stdout != self.shell.stdout:
|
| 446 |
+
self._saved_stdout = sys.stdout
|
| 447 |
+
sys.stdout = self.shell.stdout
|
| 448 |
+
|
| 449 |
+
self.reset_shell()
|
| 450 |
+
|
| 451 |
+
def tearDown(self):
|
| 452 |
+
if self._saved_stdout is not None:
|
| 453 |
+
sys.stdout = self._saved_stdout
|
| 454 |
+
|
| 455 |
+
def get_sidebar_lines(self):
|
| 456 |
+
canvas = self.shell.shell_sidebar.canvas
|
| 457 |
+
texts = list(canvas.find(tk.ALL))
|
| 458 |
+
texts_by_y_coords = {
|
| 459 |
+
canvas.bbox(text)[1]: canvas.itemcget(text, 'text')
|
| 460 |
+
for text in texts
|
| 461 |
+
}
|
| 462 |
+
line_y_coords = self.get_shell_line_y_coords()
|
| 463 |
+
return [texts_by_y_coords.get(y, None) for y in line_y_coords]
|
| 464 |
+
|
| 465 |
+
def assert_sidebar_lines_end_with(self, expected_lines):
|
| 466 |
+
self.shell.shell_sidebar.update_sidebar()
|
| 467 |
+
self.assertEqual(
|
| 468 |
+
self.get_sidebar_lines()[-len(expected_lines):],
|
| 469 |
+
expected_lines,
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
def get_shell_line_y_coords(self):
|
| 473 |
+
text = self.shell.text
|
| 474 |
+
y_coords = []
|
| 475 |
+
index = text.index("@0,0")
|
| 476 |
+
if index.split('.', 1)[1] != '0':
|
| 477 |
+
index = text.index(f"{index} +1line linestart")
|
| 478 |
+
while (lineinfo := text.dlineinfo(index)) is not None:
|
| 479 |
+
y_coords.append(lineinfo[1])
|
| 480 |
+
index = text.index(f"{index} +1line")
|
| 481 |
+
return y_coords
|
| 482 |
+
|
| 483 |
+
def get_sidebar_line_y_coords(self):
|
| 484 |
+
canvas = self.shell.shell_sidebar.canvas
|
| 485 |
+
texts = list(canvas.find(tk.ALL))
|
| 486 |
+
texts.sort(key=lambda text: canvas.bbox(text)[1])
|
| 487 |
+
return [canvas.bbox(text)[1] for text in texts]
|
| 488 |
+
|
| 489 |
+
def assert_sidebar_lines_synced(self):
|
| 490 |
+
self.assertLessEqual(
|
| 491 |
+
set(self.get_sidebar_line_y_coords()),
|
| 492 |
+
set(self.get_shell_line_y_coords()),
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
def do_input(self, input):
|
| 496 |
+
shell = self.shell
|
| 497 |
+
text = shell.text
|
| 498 |
+
for line_index, line in enumerate(input.split('\n')):
|
| 499 |
+
if line_index > 0:
|
| 500 |
+
text.event_generate('<<newline-and-indent>>')
|
| 501 |
+
text.insert('insert', line, 'stdin')
|
| 502 |
+
|
| 503 |
+
def test_initial_state(self):
|
| 504 |
+
sidebar_lines = self.get_sidebar_lines()
|
| 505 |
+
self.assertEqual(
|
| 506 |
+
sidebar_lines,
|
| 507 |
+
[None] * (len(sidebar_lines) - 1) + ['>>>'],
|
| 508 |
+
)
|
| 509 |
+
self.assert_sidebar_lines_synced()
|
| 510 |
+
|
| 511 |
+
@run_in_tk_mainloop()
|
| 512 |
+
def test_single_empty_input(self):
|
| 513 |
+
self.do_input('\n')
|
| 514 |
+
yield
|
| 515 |
+
self.assert_sidebar_lines_end_with(['>>>', '>>>'])
|
| 516 |
+
|
| 517 |
+
@run_in_tk_mainloop()
|
| 518 |
+
def test_single_line_statement(self):
|
| 519 |
+
self.do_input('1\n')
|
| 520 |
+
yield
|
| 521 |
+
self.assert_sidebar_lines_end_with(['>>>', None, '>>>'])
|
| 522 |
+
|
| 523 |
+
@run_in_tk_mainloop()
|
| 524 |
+
def test_multi_line_statement(self):
|
| 525 |
+
# Block statements are not indented because IDLE auto-indents.
|
| 526 |
+
self.do_input(dedent('''\
|
| 527 |
+
if True:
|
| 528 |
+
print(1)
|
| 529 |
+
|
| 530 |
+
'''))
|
| 531 |
+
yield
|
| 532 |
+
self.assert_sidebar_lines_end_with([
|
| 533 |
+
'>>>',
|
| 534 |
+
'...',
|
| 535 |
+
'...',
|
| 536 |
+
'...',
|
| 537 |
+
None,
|
| 538 |
+
'>>>',
|
| 539 |
+
])
|
| 540 |
+
|
| 541 |
+
@run_in_tk_mainloop()
|
| 542 |
+
def test_single_long_line_wraps(self):
|
| 543 |
+
self.do_input('1' * 200 + '\n')
|
| 544 |
+
yield
|
| 545 |
+
self.assert_sidebar_lines_end_with(['>>>', None, '>>>'])
|
| 546 |
+
self.assert_sidebar_lines_synced()
|
| 547 |
+
|
| 548 |
+
@run_in_tk_mainloop()
|
| 549 |
+
def test_squeeze_multi_line_output(self):
|
| 550 |
+
shell = self.shell
|
| 551 |
+
text = shell.text
|
| 552 |
+
|
| 553 |
+
self.do_input('print("a\\nb\\nc")\n')
|
| 554 |
+
yield
|
| 555 |
+
self.assert_sidebar_lines_end_with(['>>>', None, None, None, '>>>'])
|
| 556 |
+
|
| 557 |
+
text.mark_set('insert', f'insert -1line linestart')
|
| 558 |
+
text.event_generate('<<squeeze-current-text>>')
|
| 559 |
+
yield
|
| 560 |
+
self.assert_sidebar_lines_end_with(['>>>', None, '>>>'])
|
| 561 |
+
self.assert_sidebar_lines_synced()
|
| 562 |
+
|
| 563 |
+
shell.squeezer.expandingbuttons[0].expand()
|
| 564 |
+
yield
|
| 565 |
+
self.assert_sidebar_lines_end_with(['>>>', None, None, None, '>>>'])
|
| 566 |
+
self.assert_sidebar_lines_synced()
|
| 567 |
+
|
| 568 |
+
@run_in_tk_mainloop()
|
| 569 |
+
def test_interrupt_recall_undo_redo(self):
|
| 570 |
+
text = self.shell.text
|
| 571 |
+
# Block statements are not indented because IDLE auto-indents.
|
| 572 |
+
initial_sidebar_lines = self.get_sidebar_lines()
|
| 573 |
+
|
| 574 |
+
self.do_input(dedent('''\
|
| 575 |
+
if True:
|
| 576 |
+
print(1)
|
| 577 |
+
'''))
|
| 578 |
+
yield
|
| 579 |
+
self.assert_sidebar_lines_end_with(['>>>', '...', '...'])
|
| 580 |
+
with_block_sidebar_lines = self.get_sidebar_lines()
|
| 581 |
+
self.assertNotEqual(with_block_sidebar_lines, initial_sidebar_lines)
|
| 582 |
+
|
| 583 |
+
# Control-C
|
| 584 |
+
text.event_generate('<<interrupt-execution>>')
|
| 585 |
+
yield
|
| 586 |
+
self.assert_sidebar_lines_end_with(['>>>', '...', '...', None, '>>>'])
|
| 587 |
+
|
| 588 |
+
# Recall previous via history
|
| 589 |
+
text.event_generate('<<history-previous>>')
|
| 590 |
+
text.event_generate('<<interrupt-execution>>')
|
| 591 |
+
yield
|
| 592 |
+
self.assert_sidebar_lines_end_with(['>>>', '...', None, '>>>'])
|
| 593 |
+
|
| 594 |
+
# Recall previous via recall
|
| 595 |
+
text.mark_set('insert', text.index('insert -2l'))
|
| 596 |
+
text.event_generate('<<newline-and-indent>>')
|
| 597 |
+
yield
|
| 598 |
+
|
| 599 |
+
text.event_generate('<<undo>>')
|
| 600 |
+
yield
|
| 601 |
+
self.assert_sidebar_lines_end_with(['>>>'])
|
| 602 |
+
|
| 603 |
+
text.event_generate('<<redo>>')
|
| 604 |
+
yield
|
| 605 |
+
self.assert_sidebar_lines_end_with(['>>>', '...'])
|
| 606 |
+
|
| 607 |
+
text.event_generate('<<newline-and-indent>>')
|
| 608 |
+
text.event_generate('<<newline-and-indent>>')
|
| 609 |
+
yield
|
| 610 |
+
self.assert_sidebar_lines_end_with(
|
| 611 |
+
['>>>', '...', '...', '...', None, '>>>']
|
| 612 |
+
)
|
| 613 |
+
|
| 614 |
+
@run_in_tk_mainloop()
|
| 615 |
+
def test_very_long_wrapped_line(self):
|
| 616 |
+
with support.adjust_int_max_str_digits(11_111), \
|
| 617 |
+
swap_attr(self.shell, 'squeezer', None):
|
| 618 |
+
self.do_input('x = ' + '1'*10_000 + '\n')
|
| 619 |
+
yield
|
| 620 |
+
self.assertEqual(self.get_sidebar_lines(), ['>>>'])
|
| 621 |
+
|
| 622 |
+
def test_font(self):
|
| 623 |
+
sidebar = self.shell.shell_sidebar
|
| 624 |
+
|
| 625 |
+
test_font = 'TkTextFont'
|
| 626 |
+
|
| 627 |
+
def mock_idleconf_GetFont(root, configType, section):
|
| 628 |
+
return test_font
|
| 629 |
+
GetFont_patcher = unittest.mock.patch.object(
|
| 630 |
+
idlelib.sidebar.idleConf, 'GetFont', mock_idleconf_GetFont)
|
| 631 |
+
GetFont_patcher.start()
|
| 632 |
+
def cleanup():
|
| 633 |
+
GetFont_patcher.stop()
|
| 634 |
+
sidebar.update_font()
|
| 635 |
+
self.addCleanup(cleanup)
|
| 636 |
+
|
| 637 |
+
def get_sidebar_font():
|
| 638 |
+
canvas = sidebar.canvas
|
| 639 |
+
texts = list(canvas.find(tk.ALL))
|
| 640 |
+
fonts = {canvas.itemcget(text, 'font') for text in texts}
|
| 641 |
+
self.assertEqual(len(fonts), 1)
|
| 642 |
+
return next(iter(fonts))
|
| 643 |
+
|
| 644 |
+
self.assertNotEqual(get_sidebar_font(), test_font)
|
| 645 |
+
sidebar.update_font()
|
| 646 |
+
self.assertEqual(get_sidebar_font(), test_font)
|
| 647 |
+
|
| 648 |
+
def test_highlight_colors(self):
|
| 649 |
+
sidebar = self.shell.shell_sidebar
|
| 650 |
+
|
| 651 |
+
test_colors = {"background": '#abcdef', "foreground": '#123456'}
|
| 652 |
+
|
| 653 |
+
orig_idleConf_GetHighlight = idlelib.sidebar.idleConf.GetHighlight
|
| 654 |
+
def mock_idleconf_GetHighlight(theme, element):
|
| 655 |
+
if element in ['linenumber', 'console']:
|
| 656 |
+
return test_colors
|
| 657 |
+
return orig_idleConf_GetHighlight(theme, element)
|
| 658 |
+
GetHighlight_patcher = unittest.mock.patch.object(
|
| 659 |
+
idlelib.sidebar.idleConf, 'GetHighlight',
|
| 660 |
+
mock_idleconf_GetHighlight)
|
| 661 |
+
GetHighlight_patcher.start()
|
| 662 |
+
def cleanup():
|
| 663 |
+
GetHighlight_patcher.stop()
|
| 664 |
+
sidebar.update_colors()
|
| 665 |
+
self.addCleanup(cleanup)
|
| 666 |
+
|
| 667 |
+
def get_sidebar_colors():
|
| 668 |
+
canvas = sidebar.canvas
|
| 669 |
+
texts = list(canvas.find(tk.ALL))
|
| 670 |
+
fgs = {canvas.itemcget(text, 'fill') for text in texts}
|
| 671 |
+
self.assertEqual(len(fgs), 1)
|
| 672 |
+
fg = next(iter(fgs))
|
| 673 |
+
bg = canvas.cget('background')
|
| 674 |
+
return {"background": bg, "foreground": fg}
|
| 675 |
+
|
| 676 |
+
self.assertNotEqual(get_sidebar_colors(), test_colors)
|
| 677 |
+
sidebar.update_colors()
|
| 678 |
+
self.assertEqual(get_sidebar_colors(), test_colors)
|
| 679 |
+
|
| 680 |
+
@run_in_tk_mainloop()
|
| 681 |
+
def test_mousewheel(self):
|
| 682 |
+
sidebar = self.shell.shell_sidebar
|
| 683 |
+
text = self.shell.text
|
| 684 |
+
|
| 685 |
+
# Enter a 100-line string to scroll the shell screen down.
|
| 686 |
+
self.do_input('x = """' + '\n'*100 + '"""\n')
|
| 687 |
+
yield
|
| 688 |
+
self.assertGreater(get_lineno(text, '@0,0'), 1)
|
| 689 |
+
|
| 690 |
+
last_lineno = get_end_linenumber(text)
|
| 691 |
+
self.assertIsNotNone(text.dlineinfo(text.index(f'{last_lineno}.0')))
|
| 692 |
+
|
| 693 |
+
# Scroll up using the <MouseWheel> event.
|
| 694 |
+
# The meaning delta is platform-dependant.
|
| 695 |
+
delta = -1 if sys.platform == 'darwin' else 120
|
| 696 |
+
sidebar.canvas.event_generate('<MouseWheel>', x=0, y=0, delta=delta)
|
| 697 |
+
yield
|
| 698 |
+
self.assertIsNone(text.dlineinfo(text.index(f'{last_lineno}.0')))
|
| 699 |
+
|
| 700 |
+
# Scroll back down using the <Button-5> event.
|
| 701 |
+
sidebar.canvas.event_generate('<Button-5>', x=0, y=0)
|
| 702 |
+
yield
|
| 703 |
+
self.assertIsNotNone(text.dlineinfo(text.index(f'{last_lineno}.0')))
|
| 704 |
+
|
| 705 |
+
@run_in_tk_mainloop()
|
| 706 |
+
def test_copy(self):
|
| 707 |
+
sidebar = self.shell.shell_sidebar
|
| 708 |
+
text = self.shell.text
|
| 709 |
+
|
| 710 |
+
first_line = get_end_linenumber(text)
|
| 711 |
+
|
| 712 |
+
self.do_input(dedent('''\
|
| 713 |
+
if True:
|
| 714 |
+
print(1)
|
| 715 |
+
|
| 716 |
+
'''))
|
| 717 |
+
yield
|
| 718 |
+
|
| 719 |
+
text.tag_add('sel', f'{first_line}.0', 'end-1c')
|
| 720 |
+
selected_text = text.get('sel.first', 'sel.last')
|
| 721 |
+
self.assertTrue(selected_text.startswith('if True:\n'))
|
| 722 |
+
self.assertIn('\n1\n', selected_text)
|
| 723 |
+
|
| 724 |
+
text.event_generate('<<copy>>')
|
| 725 |
+
self.addCleanup(text.clipboard_clear)
|
| 726 |
+
|
| 727 |
+
copied_text = text.clipboard_get()
|
| 728 |
+
self.assertEqual(copied_text, selected_text)
|
| 729 |
+
|
| 730 |
+
@run_in_tk_mainloop()
|
| 731 |
+
def test_copy_with_prompts(self):
|
| 732 |
+
sidebar = self.shell.shell_sidebar
|
| 733 |
+
text = self.shell.text
|
| 734 |
+
|
| 735 |
+
first_line = get_end_linenumber(text)
|
| 736 |
+
self.do_input(dedent('''\
|
| 737 |
+
if True:
|
| 738 |
+
print(1)
|
| 739 |
+
|
| 740 |
+
'''))
|
| 741 |
+
yield
|
| 742 |
+
|
| 743 |
+
text.tag_add('sel', f'{first_line}.3', 'end-1c')
|
| 744 |
+
selected_text = text.get('sel.first', 'sel.last')
|
| 745 |
+
self.assertTrue(selected_text.startswith('True:\n'))
|
| 746 |
+
|
| 747 |
+
selected_lines_text = text.get('sel.first linestart', 'sel.last')
|
| 748 |
+
selected_lines = selected_lines_text.split('\n')
|
| 749 |
+
selected_lines.pop() # Final '' is a split artifact, not a line.
|
| 750 |
+
# Expect a block of input and a single output line.
|
| 751 |
+
expected_prompts = \
|
| 752 |
+
['>>>'] + ['...'] * (len(selected_lines) - 2) + [None]
|
| 753 |
+
selected_text_with_prompts = '\n'.join(
|
| 754 |
+
line if prompt is None else prompt + ' ' + line
|
| 755 |
+
for prompt, line in zip(expected_prompts,
|
| 756 |
+
selected_lines,
|
| 757 |
+
strict=True)
|
| 758 |
+
) + '\n'
|
| 759 |
+
|
| 760 |
+
text.event_generate('<<copy-with-prompts>>')
|
| 761 |
+
self.addCleanup(text.clipboard_clear)
|
| 762 |
+
|
| 763 |
+
copied_text = text.clipboard_get()
|
| 764 |
+
self.assertEqual(copied_text, selected_text_with_prompts)
|
| 765 |
+
|
| 766 |
+
|
| 767 |
+
if __name__ == '__main__':
|
| 768 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_squeezer.py
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test squeezer, coverage 95%"
|
| 2 |
+
|
| 3 |
+
from textwrap import dedent
|
| 4 |
+
from tkinter import Text, Tk
|
| 5 |
+
import unittest
|
| 6 |
+
from unittest.mock import Mock, NonCallableMagicMock, patch, sentinel, ANY
|
| 7 |
+
from test.support import requires
|
| 8 |
+
|
| 9 |
+
from idlelib.config import idleConf
|
| 10 |
+
from idlelib.percolator import Percolator
|
| 11 |
+
from idlelib.squeezer import count_lines_with_wrapping, ExpandingButton, \
|
| 12 |
+
Squeezer
|
| 13 |
+
from idlelib import macosx
|
| 14 |
+
from idlelib.textview import view_text
|
| 15 |
+
from idlelib.tooltip import Hovertip
|
| 16 |
+
|
| 17 |
+
SENTINEL_VALUE = sentinel.SENTINEL_VALUE
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_test_tk_root(test_instance):
|
| 21 |
+
"""Helper for tests: Create a root Tk object."""
|
| 22 |
+
requires('gui')
|
| 23 |
+
root = Tk()
|
| 24 |
+
root.withdraw()
|
| 25 |
+
|
| 26 |
+
def cleanup_root():
|
| 27 |
+
root.update_idletasks()
|
| 28 |
+
root.destroy()
|
| 29 |
+
test_instance.addCleanup(cleanup_root)
|
| 30 |
+
|
| 31 |
+
return root
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class CountLinesTest(unittest.TestCase):
|
| 35 |
+
"""Tests for the count_lines_with_wrapping function."""
|
| 36 |
+
def check(self, expected, text, linewidth):
|
| 37 |
+
return self.assertEqual(
|
| 38 |
+
expected,
|
| 39 |
+
count_lines_with_wrapping(text, linewidth),
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def test_count_empty(self):
|
| 43 |
+
"""Test with an empty string."""
|
| 44 |
+
self.assertEqual(count_lines_with_wrapping(""), 0)
|
| 45 |
+
|
| 46 |
+
def test_count_begins_with_empty_line(self):
|
| 47 |
+
"""Test with a string which begins with a newline."""
|
| 48 |
+
self.assertEqual(count_lines_with_wrapping("\ntext"), 2)
|
| 49 |
+
|
| 50 |
+
def test_count_ends_with_empty_line(self):
|
| 51 |
+
"""Test with a string which ends with a newline."""
|
| 52 |
+
self.assertEqual(count_lines_with_wrapping("text\n"), 1)
|
| 53 |
+
|
| 54 |
+
def test_count_several_lines(self):
|
| 55 |
+
"""Test with several lines of text."""
|
| 56 |
+
self.assertEqual(count_lines_with_wrapping("1\n2\n3\n"), 3)
|
| 57 |
+
|
| 58 |
+
def test_empty_lines(self):
|
| 59 |
+
self.check(expected=1, text='\n', linewidth=80)
|
| 60 |
+
self.check(expected=2, text='\n\n', linewidth=80)
|
| 61 |
+
self.check(expected=10, text='\n' * 10, linewidth=80)
|
| 62 |
+
|
| 63 |
+
def test_long_line(self):
|
| 64 |
+
self.check(expected=3, text='a' * 200, linewidth=80)
|
| 65 |
+
self.check(expected=3, text='a' * 200 + '\n', linewidth=80)
|
| 66 |
+
|
| 67 |
+
def test_several_lines_different_lengths(self):
|
| 68 |
+
text = dedent("""\
|
| 69 |
+
13 characters
|
| 70 |
+
43 is the number of characters on this line
|
| 71 |
+
|
| 72 |
+
7 chars
|
| 73 |
+
13 characters""")
|
| 74 |
+
self.check(expected=5, text=text, linewidth=80)
|
| 75 |
+
self.check(expected=5, text=text + '\n', linewidth=80)
|
| 76 |
+
self.check(expected=6, text=text, linewidth=40)
|
| 77 |
+
self.check(expected=7, text=text, linewidth=20)
|
| 78 |
+
self.check(expected=11, text=text, linewidth=10)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class SqueezerTest(unittest.TestCase):
|
| 82 |
+
"""Tests for the Squeezer class."""
|
| 83 |
+
def make_mock_editor_window(self, with_text_widget=False):
|
| 84 |
+
"""Create a mock EditorWindow instance."""
|
| 85 |
+
editwin = NonCallableMagicMock()
|
| 86 |
+
editwin.width = 80
|
| 87 |
+
|
| 88 |
+
if with_text_widget:
|
| 89 |
+
editwin.root = get_test_tk_root(self)
|
| 90 |
+
text_widget = self.make_text_widget(root=editwin.root)
|
| 91 |
+
editwin.text = editwin.per.bottom = text_widget
|
| 92 |
+
|
| 93 |
+
return editwin
|
| 94 |
+
|
| 95 |
+
def make_squeezer_instance(self, editor_window=None):
|
| 96 |
+
"""Create an actual Squeezer instance with a mock EditorWindow."""
|
| 97 |
+
if editor_window is None:
|
| 98 |
+
editor_window = self.make_mock_editor_window()
|
| 99 |
+
squeezer = Squeezer(editor_window)
|
| 100 |
+
return squeezer
|
| 101 |
+
|
| 102 |
+
def make_text_widget(self, root=None):
|
| 103 |
+
if root is None:
|
| 104 |
+
root = get_test_tk_root(self)
|
| 105 |
+
text_widget = Text(root)
|
| 106 |
+
text_widget["font"] = ('Courier', 10)
|
| 107 |
+
text_widget.mark_set("iomark", "1.0")
|
| 108 |
+
return text_widget
|
| 109 |
+
|
| 110 |
+
def set_idleconf_option_with_cleanup(self, configType, section, option, value):
|
| 111 |
+
prev_val = idleConf.GetOption(configType, section, option)
|
| 112 |
+
idleConf.SetOption(configType, section, option, value)
|
| 113 |
+
self.addCleanup(idleConf.SetOption,
|
| 114 |
+
configType, section, option, prev_val)
|
| 115 |
+
|
| 116 |
+
def test_count_lines(self):
|
| 117 |
+
"""Test Squeezer.count_lines() with various inputs."""
|
| 118 |
+
editwin = self.make_mock_editor_window()
|
| 119 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 120 |
+
|
| 121 |
+
for text_code, line_width, expected in [
|
| 122 |
+
(r"'\n'", 80, 1),
|
| 123 |
+
(r"'\n' * 3", 80, 3),
|
| 124 |
+
(r"'a' * 40 + '\n'", 80, 1),
|
| 125 |
+
(r"'a' * 80 + '\n'", 80, 1),
|
| 126 |
+
(r"'a' * 200 + '\n'", 80, 3),
|
| 127 |
+
(r"'aa\t' * 20", 80, 2),
|
| 128 |
+
(r"'aa\t' * 21", 80, 3),
|
| 129 |
+
(r"'aa\t' * 20", 40, 4),
|
| 130 |
+
]:
|
| 131 |
+
with self.subTest(text_code=text_code,
|
| 132 |
+
line_width=line_width,
|
| 133 |
+
expected=expected):
|
| 134 |
+
text = eval(text_code)
|
| 135 |
+
with patch.object(editwin, 'width', line_width):
|
| 136 |
+
self.assertEqual(squeezer.count_lines(text), expected)
|
| 137 |
+
|
| 138 |
+
def test_init(self):
|
| 139 |
+
"""Test the creation of Squeezer instances."""
|
| 140 |
+
editwin = self.make_mock_editor_window()
|
| 141 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 142 |
+
self.assertIs(squeezer.editwin, editwin)
|
| 143 |
+
self.assertEqual(squeezer.expandingbuttons, [])
|
| 144 |
+
|
| 145 |
+
def test_write_no_tags(self):
|
| 146 |
+
"""Test Squeezer's overriding of the EditorWindow's write() method."""
|
| 147 |
+
editwin = self.make_mock_editor_window()
|
| 148 |
+
for text in ['', 'TEXT', 'LONG TEXT' * 1000, 'MANY_LINES\n' * 100]:
|
| 149 |
+
editwin.write = orig_write = Mock(return_value=SENTINEL_VALUE)
|
| 150 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 151 |
+
|
| 152 |
+
self.assertEqual(squeezer.editwin.write(text, ()), SENTINEL_VALUE)
|
| 153 |
+
self.assertEqual(orig_write.call_count, 1)
|
| 154 |
+
orig_write.assert_called_with(text, ())
|
| 155 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 156 |
+
|
| 157 |
+
def test_write_not_stdout(self):
|
| 158 |
+
"""Test Squeezer's overriding of the EditorWindow's write() method."""
|
| 159 |
+
for text in ['', 'TEXT', 'LONG TEXT' * 1000, 'MANY_LINES\n' * 100]:
|
| 160 |
+
editwin = self.make_mock_editor_window()
|
| 161 |
+
editwin.write.return_value = SENTINEL_VALUE
|
| 162 |
+
orig_write = editwin.write
|
| 163 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 164 |
+
|
| 165 |
+
self.assertEqual(squeezer.editwin.write(text, "stderr"),
|
| 166 |
+
SENTINEL_VALUE)
|
| 167 |
+
self.assertEqual(orig_write.call_count, 1)
|
| 168 |
+
orig_write.assert_called_with(text, "stderr")
|
| 169 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 170 |
+
|
| 171 |
+
def test_write_stdout(self):
|
| 172 |
+
"""Test Squeezer's overriding of the EditorWindow's write() method."""
|
| 173 |
+
requires('gui')
|
| 174 |
+
editwin = self.make_mock_editor_window()
|
| 175 |
+
|
| 176 |
+
for text in ['', 'TEXT']:
|
| 177 |
+
editwin.write = orig_write = Mock(return_value=SENTINEL_VALUE)
|
| 178 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 179 |
+
squeezer.auto_squeeze_min_lines = 50
|
| 180 |
+
|
| 181 |
+
self.assertEqual(squeezer.editwin.write(text, "stdout"),
|
| 182 |
+
SENTINEL_VALUE)
|
| 183 |
+
self.assertEqual(orig_write.call_count, 1)
|
| 184 |
+
orig_write.assert_called_with(text, "stdout")
|
| 185 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 186 |
+
|
| 187 |
+
for text in ['LONG TEXT' * 1000, 'MANY_LINES\n' * 100]:
|
| 188 |
+
editwin.write = orig_write = Mock(return_value=SENTINEL_VALUE)
|
| 189 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 190 |
+
squeezer.auto_squeeze_min_lines = 50
|
| 191 |
+
|
| 192 |
+
self.assertEqual(squeezer.editwin.write(text, "stdout"), None)
|
| 193 |
+
self.assertEqual(orig_write.call_count, 0)
|
| 194 |
+
self.assertEqual(len(squeezer.expandingbuttons), 1)
|
| 195 |
+
|
| 196 |
+
def test_auto_squeeze(self):
|
| 197 |
+
"""Test that the auto-squeezing creates an ExpandingButton properly."""
|
| 198 |
+
editwin = self.make_mock_editor_window(with_text_widget=True)
|
| 199 |
+
text_widget = editwin.text
|
| 200 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 201 |
+
squeezer.auto_squeeze_min_lines = 5
|
| 202 |
+
squeezer.count_lines = Mock(return_value=6)
|
| 203 |
+
|
| 204 |
+
editwin.write('TEXT\n'*6, "stdout")
|
| 205 |
+
self.assertEqual(text_widget.get('1.0', 'end'), '\n')
|
| 206 |
+
self.assertEqual(len(squeezer.expandingbuttons), 1)
|
| 207 |
+
|
| 208 |
+
def test_squeeze_current_text(self):
|
| 209 |
+
"""Test the squeeze_current_text method."""
|
| 210 |
+
# Squeezing text should work for both stdout and stderr.
|
| 211 |
+
for tag_name in ["stdout", "stderr"]:
|
| 212 |
+
editwin = self.make_mock_editor_window(with_text_widget=True)
|
| 213 |
+
text_widget = editwin.text
|
| 214 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 215 |
+
squeezer.count_lines = Mock(return_value=6)
|
| 216 |
+
|
| 217 |
+
# Prepare some text in the Text widget.
|
| 218 |
+
text_widget.insert("1.0", "SOME\nTEXT\n", tag_name)
|
| 219 |
+
text_widget.mark_set("insert", "1.0")
|
| 220 |
+
self.assertEqual(text_widget.get('1.0', 'end'), 'SOME\nTEXT\n\n')
|
| 221 |
+
|
| 222 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 223 |
+
|
| 224 |
+
# Test squeezing the current text.
|
| 225 |
+
retval = squeezer.squeeze_current_text()
|
| 226 |
+
self.assertEqual(retval, "break")
|
| 227 |
+
self.assertEqual(text_widget.get('1.0', 'end'), '\n\n')
|
| 228 |
+
self.assertEqual(len(squeezer.expandingbuttons), 1)
|
| 229 |
+
self.assertEqual(squeezer.expandingbuttons[0].s, 'SOME\nTEXT')
|
| 230 |
+
|
| 231 |
+
# Test that expanding the squeezed text works and afterwards
|
| 232 |
+
# the Text widget contains the original text.
|
| 233 |
+
squeezer.expandingbuttons[0].expand()
|
| 234 |
+
self.assertEqual(text_widget.get('1.0', 'end'), 'SOME\nTEXT\n\n')
|
| 235 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 236 |
+
|
| 237 |
+
def test_squeeze_current_text_no_allowed_tags(self):
|
| 238 |
+
"""Test that the event doesn't squeeze text without a relevant tag."""
|
| 239 |
+
editwin = self.make_mock_editor_window(with_text_widget=True)
|
| 240 |
+
text_widget = editwin.text
|
| 241 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 242 |
+
squeezer.count_lines = Mock(return_value=6)
|
| 243 |
+
|
| 244 |
+
# Prepare some text in the Text widget.
|
| 245 |
+
text_widget.insert("1.0", "SOME\nTEXT\n", "TAG")
|
| 246 |
+
text_widget.mark_set("insert", "1.0")
|
| 247 |
+
self.assertEqual(text_widget.get('1.0', 'end'), 'SOME\nTEXT\n\n')
|
| 248 |
+
|
| 249 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 250 |
+
|
| 251 |
+
# Test squeezing the current text.
|
| 252 |
+
retval = squeezer.squeeze_current_text()
|
| 253 |
+
self.assertEqual(retval, "break")
|
| 254 |
+
self.assertEqual(text_widget.get('1.0', 'end'), 'SOME\nTEXT\n\n')
|
| 255 |
+
self.assertEqual(len(squeezer.expandingbuttons), 0)
|
| 256 |
+
|
| 257 |
+
def test_squeeze_text_before_existing_squeezed_text(self):
|
| 258 |
+
"""Test squeezing text before existing squeezed text."""
|
| 259 |
+
editwin = self.make_mock_editor_window(with_text_widget=True)
|
| 260 |
+
text_widget = editwin.text
|
| 261 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 262 |
+
squeezer.count_lines = Mock(return_value=6)
|
| 263 |
+
|
| 264 |
+
# Prepare some text in the Text widget and squeeze it.
|
| 265 |
+
text_widget.insert("1.0", "SOME\nTEXT\n", "stdout")
|
| 266 |
+
text_widget.mark_set("insert", "1.0")
|
| 267 |
+
squeezer.squeeze_current_text()
|
| 268 |
+
self.assertEqual(len(squeezer.expandingbuttons), 1)
|
| 269 |
+
|
| 270 |
+
# Test squeezing the current text.
|
| 271 |
+
text_widget.insert("1.0", "MORE\nSTUFF\n", "stdout")
|
| 272 |
+
text_widget.mark_set("insert", "1.0")
|
| 273 |
+
retval = squeezer.squeeze_current_text()
|
| 274 |
+
self.assertEqual(retval, "break")
|
| 275 |
+
self.assertEqual(text_widget.get('1.0', 'end'), '\n\n\n')
|
| 276 |
+
self.assertEqual(len(squeezer.expandingbuttons), 2)
|
| 277 |
+
self.assertTrue(text_widget.compare(
|
| 278 |
+
squeezer.expandingbuttons[0],
|
| 279 |
+
'<',
|
| 280 |
+
squeezer.expandingbuttons[1],
|
| 281 |
+
))
|
| 282 |
+
|
| 283 |
+
def test_reload(self):
|
| 284 |
+
"""Test the reload() class-method."""
|
| 285 |
+
editwin = self.make_mock_editor_window(with_text_widget=True)
|
| 286 |
+
squeezer = self.make_squeezer_instance(editwin)
|
| 287 |
+
|
| 288 |
+
orig_auto_squeeze_min_lines = squeezer.auto_squeeze_min_lines
|
| 289 |
+
|
| 290 |
+
# Increase auto-squeeze-min-lines.
|
| 291 |
+
new_auto_squeeze_min_lines = orig_auto_squeeze_min_lines + 10
|
| 292 |
+
self.set_idleconf_option_with_cleanup(
|
| 293 |
+
'main', 'PyShell', 'auto-squeeze-min-lines',
|
| 294 |
+
str(new_auto_squeeze_min_lines))
|
| 295 |
+
|
| 296 |
+
Squeezer.reload()
|
| 297 |
+
self.assertEqual(squeezer.auto_squeeze_min_lines,
|
| 298 |
+
new_auto_squeeze_min_lines)
|
| 299 |
+
|
| 300 |
+
def test_reload_no_squeezer_instances(self):
|
| 301 |
+
"""Test that Squeezer.reload() runs without any instances existing."""
|
| 302 |
+
Squeezer.reload()
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
class ExpandingButtonTest(unittest.TestCase):
|
| 306 |
+
"""Tests for the ExpandingButton class."""
|
| 307 |
+
# In these tests the squeezer instance is a mock, but actual tkinter
|
| 308 |
+
# Text and Button instances are created.
|
| 309 |
+
def make_mock_squeezer(self):
|
| 310 |
+
"""Helper for tests: Create a mock Squeezer object."""
|
| 311 |
+
root = get_test_tk_root(self)
|
| 312 |
+
squeezer = Mock()
|
| 313 |
+
squeezer.editwin.text = Text(root)
|
| 314 |
+
squeezer.editwin.per = Percolator(squeezer.editwin.text)
|
| 315 |
+
self.addCleanup(squeezer.editwin.per.close)
|
| 316 |
+
|
| 317 |
+
# Set default values for the configuration settings.
|
| 318 |
+
squeezer.auto_squeeze_min_lines = 50
|
| 319 |
+
return squeezer
|
| 320 |
+
|
| 321 |
+
@patch('idlelib.squeezer.Hovertip', autospec=Hovertip)
|
| 322 |
+
def test_init(self, MockHovertip):
|
| 323 |
+
"""Test the simplest creation of an ExpandingButton."""
|
| 324 |
+
squeezer = self.make_mock_squeezer()
|
| 325 |
+
text_widget = squeezer.editwin.text
|
| 326 |
+
|
| 327 |
+
expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
|
| 328 |
+
self.assertEqual(expandingbutton.s, 'TEXT')
|
| 329 |
+
|
| 330 |
+
# Check that the underlying tkinter.Button is properly configured.
|
| 331 |
+
self.assertEqual(expandingbutton.master, text_widget)
|
| 332 |
+
self.assertTrue('50 lines' in expandingbutton.cget('text'))
|
| 333 |
+
|
| 334 |
+
# Check that the text widget still contains no text.
|
| 335 |
+
self.assertEqual(text_widget.get('1.0', 'end'), '\n')
|
| 336 |
+
|
| 337 |
+
# Check that the mouse events are bound.
|
| 338 |
+
self.assertIn('<Double-Button-1>', expandingbutton.bind())
|
| 339 |
+
right_button_code = '<Button-%s>' % ('2' if macosx.isAquaTk() else '3')
|
| 340 |
+
self.assertIn(right_button_code, expandingbutton.bind())
|
| 341 |
+
|
| 342 |
+
# Check that ToolTip was called once, with appropriate values.
|
| 343 |
+
self.assertEqual(MockHovertip.call_count, 1)
|
| 344 |
+
MockHovertip.assert_called_with(expandingbutton, ANY, hover_delay=ANY)
|
| 345 |
+
|
| 346 |
+
# Check that 'right-click' appears in the tooltip text.
|
| 347 |
+
tooltip_text = MockHovertip.call_args[0][1]
|
| 348 |
+
self.assertIn('right-click', tooltip_text.lower())
|
| 349 |
+
|
| 350 |
+
def test_expand(self):
|
| 351 |
+
"""Test the expand event."""
|
| 352 |
+
squeezer = self.make_mock_squeezer()
|
| 353 |
+
expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
|
| 354 |
+
|
| 355 |
+
# Insert the button into the text widget
|
| 356 |
+
# (this is normally done by the Squeezer class).
|
| 357 |
+
text_widget = squeezer.editwin.text
|
| 358 |
+
text_widget.window_create("1.0", window=expandingbutton)
|
| 359 |
+
|
| 360 |
+
# trigger the expand event
|
| 361 |
+
retval = expandingbutton.expand(event=Mock())
|
| 362 |
+
self.assertEqual(retval, None)
|
| 363 |
+
|
| 364 |
+
# Check that the text was inserted into the text widget.
|
| 365 |
+
self.assertEqual(text_widget.get('1.0', 'end'), 'TEXT\n')
|
| 366 |
+
|
| 367 |
+
# Check that the 'TAGS' tag was set on the inserted text.
|
| 368 |
+
text_end_index = text_widget.index('end-1c')
|
| 369 |
+
self.assertEqual(text_widget.get('1.0', text_end_index), 'TEXT')
|
| 370 |
+
self.assertEqual(text_widget.tag_nextrange('TAGS', '1.0'),
|
| 371 |
+
('1.0', text_end_index))
|
| 372 |
+
|
| 373 |
+
# Check that the button removed itself from squeezer.expandingbuttons.
|
| 374 |
+
self.assertEqual(squeezer.expandingbuttons.remove.call_count, 1)
|
| 375 |
+
squeezer.expandingbuttons.remove.assert_called_with(expandingbutton)
|
| 376 |
+
|
| 377 |
+
def test_expand_dangerous_oupput(self):
|
| 378 |
+
"""Test that expanding very long output asks user for confirmation."""
|
| 379 |
+
squeezer = self.make_mock_squeezer()
|
| 380 |
+
text = 'a' * 10**5
|
| 381 |
+
expandingbutton = ExpandingButton(text, 'TAGS', 50, squeezer)
|
| 382 |
+
expandingbutton.set_is_dangerous()
|
| 383 |
+
self.assertTrue(expandingbutton.is_dangerous)
|
| 384 |
+
|
| 385 |
+
# Insert the button into the text widget
|
| 386 |
+
# (this is normally done by the Squeezer class).
|
| 387 |
+
text_widget = expandingbutton.text
|
| 388 |
+
text_widget.window_create("1.0", window=expandingbutton)
|
| 389 |
+
|
| 390 |
+
# Patch the message box module to always return False.
|
| 391 |
+
with patch('idlelib.squeezer.messagebox') as mock_msgbox:
|
| 392 |
+
mock_msgbox.askokcancel.return_value = False
|
| 393 |
+
mock_msgbox.askyesno.return_value = False
|
| 394 |
+
# Trigger the expand event.
|
| 395 |
+
retval = expandingbutton.expand(event=Mock())
|
| 396 |
+
|
| 397 |
+
# Check that the event chain was broken and no text was inserted.
|
| 398 |
+
self.assertEqual(retval, 'break')
|
| 399 |
+
self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), '')
|
| 400 |
+
|
| 401 |
+
# Patch the message box module to always return True.
|
| 402 |
+
with patch('idlelib.squeezer.messagebox') as mock_msgbox:
|
| 403 |
+
mock_msgbox.askokcancel.return_value = True
|
| 404 |
+
mock_msgbox.askyesno.return_value = True
|
| 405 |
+
# Trigger the expand event.
|
| 406 |
+
retval = expandingbutton.expand(event=Mock())
|
| 407 |
+
|
| 408 |
+
# Check that the event chain wasn't broken and the text was inserted.
|
| 409 |
+
self.assertEqual(retval, None)
|
| 410 |
+
self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), text)
|
| 411 |
+
|
| 412 |
+
def test_copy(self):
|
| 413 |
+
"""Test the copy event."""
|
| 414 |
+
# Testing with the actual clipboard proved problematic, so this
|
| 415 |
+
# test replaces the clipboard manipulation functions with mocks
|
| 416 |
+
# and checks that they are called appropriately.
|
| 417 |
+
squeezer = self.make_mock_squeezer()
|
| 418 |
+
expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
|
| 419 |
+
expandingbutton.clipboard_clear = Mock()
|
| 420 |
+
expandingbutton.clipboard_append = Mock()
|
| 421 |
+
|
| 422 |
+
# Trigger the copy event.
|
| 423 |
+
retval = expandingbutton.copy(event=Mock())
|
| 424 |
+
self.assertEqual(retval, None)
|
| 425 |
+
|
| 426 |
+
# Vheck that the expanding button called clipboard_clear() and
|
| 427 |
+
# clipboard_append('TEXT') once each.
|
| 428 |
+
self.assertEqual(expandingbutton.clipboard_clear.call_count, 1)
|
| 429 |
+
self.assertEqual(expandingbutton.clipboard_append.call_count, 1)
|
| 430 |
+
expandingbutton.clipboard_append.assert_called_with('TEXT')
|
| 431 |
+
|
| 432 |
+
def test_view(self):
|
| 433 |
+
"""Test the view event."""
|
| 434 |
+
squeezer = self.make_mock_squeezer()
|
| 435 |
+
expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
|
| 436 |
+
expandingbutton.selection_own = Mock()
|
| 437 |
+
|
| 438 |
+
with patch('idlelib.squeezer.view_text', autospec=view_text)\
|
| 439 |
+
as mock_view_text:
|
| 440 |
+
# Trigger the view event.
|
| 441 |
+
expandingbutton.view(event=Mock())
|
| 442 |
+
|
| 443 |
+
# Check that the expanding button called view_text.
|
| 444 |
+
self.assertEqual(mock_view_text.call_count, 1)
|
| 445 |
+
|
| 446 |
+
# Check that the proper text was passed.
|
| 447 |
+
self.assertEqual(mock_view_text.call_args[0][2], 'TEXT')
|
| 448 |
+
|
| 449 |
+
def test_rmenu(self):
|
| 450 |
+
"""Test the context menu."""
|
| 451 |
+
squeezer = self.make_mock_squeezer()
|
| 452 |
+
expandingbutton = ExpandingButton('TEXT', 'TAGS', 50, squeezer)
|
| 453 |
+
with patch('tkinter.Menu') as mock_Menu:
|
| 454 |
+
mock_menu = Mock()
|
| 455 |
+
mock_Menu.return_value = mock_menu
|
| 456 |
+
mock_event = Mock()
|
| 457 |
+
mock_event.x = 10
|
| 458 |
+
mock_event.y = 10
|
| 459 |
+
expandingbutton.context_menu_event(event=mock_event)
|
| 460 |
+
self.assertEqual(mock_menu.add_command.call_count,
|
| 461 |
+
len(expandingbutton.rmenu_specs))
|
| 462 |
+
for label, *data in expandingbutton.rmenu_specs:
|
| 463 |
+
mock_menu.add_command.assert_any_call(label=label, command=ANY)
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
if __name__ == '__main__':
|
| 467 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_stackviewer.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test stackviewer, coverage 63%."
|
| 2 |
+
|
| 3 |
+
from idlelib import stackviewer
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
from tkinter import Tk
|
| 7 |
+
|
| 8 |
+
from idlelib.tree import TreeNode, ScrolledCanvas
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class StackBrowserTest(unittest.TestCase):
|
| 13 |
+
|
| 14 |
+
@classmethod
|
| 15 |
+
def setUpClass(cls):
|
| 16 |
+
svs = stackviewer.sys
|
| 17 |
+
try:
|
| 18 |
+
abc
|
| 19 |
+
except NameError:
|
| 20 |
+
svs.last_type, svs.last_value, svs.last_traceback = (
|
| 21 |
+
sys.exc_info())
|
| 22 |
+
|
| 23 |
+
requires('gui')
|
| 24 |
+
cls.root = Tk()
|
| 25 |
+
cls.root.withdraw()
|
| 26 |
+
|
| 27 |
+
@classmethod
|
| 28 |
+
def tearDownClass(cls):
|
| 29 |
+
svs = stackviewer.sys
|
| 30 |
+
del svs.last_traceback, svs.last_type, svs.last_value
|
| 31 |
+
|
| 32 |
+
cls.root.update_idletasks()
|
| 33 |
+
## for id in cls.root.tk.call('after', 'info'):
|
| 34 |
+
## cls.root.after_cancel(id) # Need for EditorWindow.
|
| 35 |
+
cls.root.destroy()
|
| 36 |
+
del cls.root
|
| 37 |
+
|
| 38 |
+
def test_init(self):
|
| 39 |
+
sb = stackviewer.StackBrowser(self.root)
|
| 40 |
+
isi = self.assertIsInstance
|
| 41 |
+
isi(stackviewer.sc, ScrolledCanvas)
|
| 42 |
+
isi(stackviewer.item, stackviewer.StackTreeItem)
|
| 43 |
+
isi(stackviewer.node, TreeNode)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_text.py
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
''' Test mock_tk.Text class against tkinter.Text class
|
| 2 |
+
|
| 3 |
+
Run same tests with both by creating a mixin class.
|
| 4 |
+
'''
|
| 5 |
+
import unittest
|
| 6 |
+
from test.support import requires
|
| 7 |
+
from _tkinter import TclError
|
| 8 |
+
|
| 9 |
+
class TextTest:
|
| 10 |
+
"Define items common to both sets of tests."
|
| 11 |
+
|
| 12 |
+
hw = 'hello\nworld' # Several tests insert this after initialization.
|
| 13 |
+
hwn = hw+'\n' # \n present at initialization, before insert
|
| 14 |
+
|
| 15 |
+
# setUpClass defines cls.Text and maybe cls.root.
|
| 16 |
+
# setUp defines self.text from Text and maybe root.
|
| 17 |
+
|
| 18 |
+
def test_init(self):
|
| 19 |
+
self.assertEqual(self.text.get('1.0'), '\n')
|
| 20 |
+
self.assertEqual(self.text.get('end'), '')
|
| 21 |
+
|
| 22 |
+
def test_index_empty(self):
|
| 23 |
+
index = self.text.index
|
| 24 |
+
|
| 25 |
+
for dex in (-1.0, 0.3, '1.-1', '1.0', '1.0 lineend', '1.end', '1.33',
|
| 26 |
+
'insert'):
|
| 27 |
+
self.assertEqual(index(dex), '1.0')
|
| 28 |
+
|
| 29 |
+
for dex in 'end', 2.0, '2.1', '33.44':
|
| 30 |
+
self.assertEqual(index(dex), '2.0')
|
| 31 |
+
|
| 32 |
+
def test_index_data(self):
|
| 33 |
+
index = self.text.index
|
| 34 |
+
self.text.insert('1.0', self.hw)
|
| 35 |
+
|
| 36 |
+
for dex in -1.0, 0.3, '1.-1', '1.0':
|
| 37 |
+
self.assertEqual(index(dex), '1.0')
|
| 38 |
+
|
| 39 |
+
for dex in '1.0 lineend', '1.end', '1.33':
|
| 40 |
+
self.assertEqual(index(dex), '1.5')
|
| 41 |
+
|
| 42 |
+
for dex in 'end', '33.44':
|
| 43 |
+
self.assertEqual(index(dex), '3.0')
|
| 44 |
+
|
| 45 |
+
def test_get(self):
|
| 46 |
+
get = self.text.get
|
| 47 |
+
Equal = self.assertEqual
|
| 48 |
+
self.text.insert('1.0', self.hw)
|
| 49 |
+
|
| 50 |
+
Equal(get('end'), '')
|
| 51 |
+
Equal(get('end', 'end'), '')
|
| 52 |
+
Equal(get('1.0'), 'h')
|
| 53 |
+
Equal(get('1.0', '1.1'), 'h')
|
| 54 |
+
Equal(get('1.0', '1.3'), 'hel')
|
| 55 |
+
Equal(get('1.1', '1.3'), 'el')
|
| 56 |
+
Equal(get('1.0', '1.0 lineend'), 'hello')
|
| 57 |
+
Equal(get('1.0', '1.10'), 'hello')
|
| 58 |
+
Equal(get('1.0 lineend'), '\n')
|
| 59 |
+
Equal(get('1.1', '2.3'), 'ello\nwor')
|
| 60 |
+
Equal(get('1.0', '2.5'), self.hw)
|
| 61 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 62 |
+
Equal(get('0.0', '5.0'), self.hwn)
|
| 63 |
+
|
| 64 |
+
def test_insert(self):
|
| 65 |
+
insert = self.text.insert
|
| 66 |
+
get = self.text.get
|
| 67 |
+
Equal = self.assertEqual
|
| 68 |
+
|
| 69 |
+
insert('1.0', self.hw)
|
| 70 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 71 |
+
|
| 72 |
+
insert('1.0', '') # nothing
|
| 73 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 74 |
+
|
| 75 |
+
insert('1.0', '*')
|
| 76 |
+
Equal(get('1.0', 'end'), '*hello\nworld\n')
|
| 77 |
+
|
| 78 |
+
insert('1.0 lineend', '*')
|
| 79 |
+
Equal(get('1.0', 'end'), '*hello*\nworld\n')
|
| 80 |
+
|
| 81 |
+
insert('2.3', '*')
|
| 82 |
+
Equal(get('1.0', 'end'), '*hello*\nwor*ld\n')
|
| 83 |
+
|
| 84 |
+
insert('end', 'x')
|
| 85 |
+
Equal(get('1.0', 'end'), '*hello*\nwor*ldx\n')
|
| 86 |
+
|
| 87 |
+
insert('1.4', 'x\n')
|
| 88 |
+
Equal(get('1.0', 'end'), '*helx\nlo*\nwor*ldx\n')
|
| 89 |
+
|
| 90 |
+
def test_no_delete(self):
|
| 91 |
+
# if index1 == 'insert' or 'end' or >= end, there is no deletion
|
| 92 |
+
delete = self.text.delete
|
| 93 |
+
get = self.text.get
|
| 94 |
+
Equal = self.assertEqual
|
| 95 |
+
self.text.insert('1.0', self.hw)
|
| 96 |
+
|
| 97 |
+
delete('insert')
|
| 98 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 99 |
+
|
| 100 |
+
delete('end')
|
| 101 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 102 |
+
|
| 103 |
+
delete('insert', 'end')
|
| 104 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 105 |
+
|
| 106 |
+
delete('insert', '5.5')
|
| 107 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 108 |
+
|
| 109 |
+
delete('1.4', '1.0')
|
| 110 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 111 |
+
|
| 112 |
+
delete('1.4', '1.4')
|
| 113 |
+
Equal(get('1.0', 'end'), self.hwn)
|
| 114 |
+
|
| 115 |
+
def test_delete_char(self):
|
| 116 |
+
delete = self.text.delete
|
| 117 |
+
get = self.text.get
|
| 118 |
+
Equal = self.assertEqual
|
| 119 |
+
self.text.insert('1.0', self.hw)
|
| 120 |
+
|
| 121 |
+
delete('1.0')
|
| 122 |
+
Equal(get('1.0', '1.end'), 'ello')
|
| 123 |
+
|
| 124 |
+
delete('1.0', '1.1')
|
| 125 |
+
Equal(get('1.0', '1.end'), 'llo')
|
| 126 |
+
|
| 127 |
+
# delete \n and combine 2 lines into 1
|
| 128 |
+
delete('1.end')
|
| 129 |
+
Equal(get('1.0', '1.end'), 'lloworld')
|
| 130 |
+
|
| 131 |
+
self.text.insert('1.3', '\n')
|
| 132 |
+
delete('1.10')
|
| 133 |
+
Equal(get('1.0', '1.end'), 'lloworld')
|
| 134 |
+
|
| 135 |
+
self.text.insert('1.3', '\n')
|
| 136 |
+
delete('1.3', '2.0')
|
| 137 |
+
Equal(get('1.0', '1.end'), 'lloworld')
|
| 138 |
+
|
| 139 |
+
def test_delete_slice(self):
|
| 140 |
+
delete = self.text.delete
|
| 141 |
+
get = self.text.get
|
| 142 |
+
Equal = self.assertEqual
|
| 143 |
+
self.text.insert('1.0', self.hw)
|
| 144 |
+
|
| 145 |
+
delete('1.0', '1.0 lineend')
|
| 146 |
+
Equal(get('1.0', 'end'), '\nworld\n')
|
| 147 |
+
|
| 148 |
+
delete('1.0', 'end')
|
| 149 |
+
Equal(get('1.0', 'end'), '\n')
|
| 150 |
+
|
| 151 |
+
self.text.insert('1.0', self.hw)
|
| 152 |
+
delete('1.0', '2.0')
|
| 153 |
+
Equal(get('1.0', 'end'), 'world\n')
|
| 154 |
+
|
| 155 |
+
delete('1.0', 'end')
|
| 156 |
+
Equal(get('1.0', 'end'), '\n')
|
| 157 |
+
|
| 158 |
+
self.text.insert('1.0', self.hw)
|
| 159 |
+
delete('1.2', '2.3')
|
| 160 |
+
Equal(get('1.0', 'end'), 'held\n')
|
| 161 |
+
|
| 162 |
+
def test_multiple_lines(self): # insert and delete
|
| 163 |
+
self.text.insert('1.0', 'hello')
|
| 164 |
+
|
| 165 |
+
self.text.insert('1.3', '1\n2\n3\n4\n5')
|
| 166 |
+
self.assertEqual(self.text.get('1.0', 'end'), 'hel1\n2\n3\n4\n5lo\n')
|
| 167 |
+
|
| 168 |
+
self.text.delete('1.3', '5.1')
|
| 169 |
+
self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')
|
| 170 |
+
|
| 171 |
+
def test_compare(self):
|
| 172 |
+
compare = self.text.compare
|
| 173 |
+
Equal = self.assertEqual
|
| 174 |
+
# need data so indexes not squished to 1,0
|
| 175 |
+
self.text.insert('1.0', 'First\nSecond\nThird\n')
|
| 176 |
+
|
| 177 |
+
self.assertRaises(TclError, compare, '2.2', 'op', '2.2')
|
| 178 |
+
|
| 179 |
+
for op, less1, less0, equal, greater0, greater1 in (
|
| 180 |
+
('<', True, True, False, False, False),
|
| 181 |
+
('<=', True, True, True, False, False),
|
| 182 |
+
('>', False, False, False, True, True),
|
| 183 |
+
('>=', False, False, True, True, True),
|
| 184 |
+
('==', False, False, True, False, False),
|
| 185 |
+
('!=', True, True, False, True, True),
|
| 186 |
+
):
|
| 187 |
+
Equal(compare('1.1', op, '2.2'), less1, op)
|
| 188 |
+
Equal(compare('2.1', op, '2.2'), less0, op)
|
| 189 |
+
Equal(compare('2.2', op, '2.2'), equal, op)
|
| 190 |
+
Equal(compare('2.3', op, '2.2'), greater0, op)
|
| 191 |
+
Equal(compare('3.3', op, '2.2'), greater1, op)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
class MockTextTest(TextTest, unittest.TestCase):
|
| 195 |
+
|
| 196 |
+
@classmethod
|
| 197 |
+
def setUpClass(cls):
|
| 198 |
+
from idlelib.idle_test.mock_tk import Text
|
| 199 |
+
cls.Text = Text
|
| 200 |
+
|
| 201 |
+
def setUp(self):
|
| 202 |
+
self.text = self.Text()
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def test_decode(self):
|
| 206 |
+
# test endflags (-1, 0) not tested by test_index (which uses +1)
|
| 207 |
+
decode = self.text._decode
|
| 208 |
+
Equal = self.assertEqual
|
| 209 |
+
self.text.insert('1.0', self.hw)
|
| 210 |
+
|
| 211 |
+
Equal(decode('end', -1), (2, 5))
|
| 212 |
+
Equal(decode('3.1', -1), (2, 5))
|
| 213 |
+
Equal(decode('end', 0), (2, 6))
|
| 214 |
+
Equal(decode('3.1', 0), (2, 6))
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
class TkTextTest(TextTest, unittest.TestCase):
|
| 218 |
+
|
| 219 |
+
@classmethod
|
| 220 |
+
def setUpClass(cls):
|
| 221 |
+
requires('gui')
|
| 222 |
+
from tkinter import Tk, Text
|
| 223 |
+
cls.Text = Text
|
| 224 |
+
cls.root = Tk()
|
| 225 |
+
|
| 226 |
+
@classmethod
|
| 227 |
+
def tearDownClass(cls):
|
| 228 |
+
cls.root.destroy()
|
| 229 |
+
del cls.root
|
| 230 |
+
|
| 231 |
+
def setUp(self):
|
| 232 |
+
self.text = self.Text(self.root)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
if __name__ == '__main__':
|
| 236 |
+
unittest.main(verbosity=2, exit=False)
|
llava/lib/python3.10/idlelib/idle_test/test_textview.py
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test textview, coverage 100%.
|
| 2 |
+
|
| 3 |
+
Since all methods and functions create (or destroy) a ViewWindow, which
|
| 4 |
+
is a widget containing a widget, etcetera, all tests must be gui tests.
|
| 5 |
+
Using mock Text would not change this. Other mocks are used to retrieve
|
| 6 |
+
information about calls.
|
| 7 |
+
"""
|
| 8 |
+
from idlelib import textview as tv
|
| 9 |
+
from test.support import requires
|
| 10 |
+
requires('gui')
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import unittest
|
| 14 |
+
from tkinter import Tk, TclError, CHAR, NONE, WORD
|
| 15 |
+
from tkinter.ttk import Button
|
| 16 |
+
from idlelib.idle_test.mock_idle import Func
|
| 17 |
+
from idlelib.idle_test.mock_tk import Mbox_func
|
| 18 |
+
|
| 19 |
+
def setUpModule():
|
| 20 |
+
global root
|
| 21 |
+
root = Tk()
|
| 22 |
+
root.withdraw()
|
| 23 |
+
|
| 24 |
+
def tearDownModule():
|
| 25 |
+
global root
|
| 26 |
+
root.update_idletasks()
|
| 27 |
+
root.destroy()
|
| 28 |
+
del root
|
| 29 |
+
|
| 30 |
+
# If we call ViewWindow or wrapper functions with defaults
|
| 31 |
+
# modal=True, _utest=False, test hangs on call to wait_window.
|
| 32 |
+
# Have also gotten tk error 'can't invoke "event" command'.
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class VW(tv.ViewWindow): # Used in ViewWindowTest.
|
| 36 |
+
transient = Func()
|
| 37 |
+
grab_set = Func()
|
| 38 |
+
wait_window = Func()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Call wrapper class VW with mock wait_window.
|
| 42 |
+
class ViewWindowTest(unittest.TestCase):
|
| 43 |
+
|
| 44 |
+
def setUp(self):
|
| 45 |
+
VW.transient.__init__()
|
| 46 |
+
VW.grab_set.__init__()
|
| 47 |
+
VW.wait_window.__init__()
|
| 48 |
+
|
| 49 |
+
def test_init_modal(self):
|
| 50 |
+
view = VW(root, 'Title', 'test text')
|
| 51 |
+
self.assertTrue(VW.transient.called)
|
| 52 |
+
self.assertTrue(VW.grab_set.called)
|
| 53 |
+
self.assertTrue(VW.wait_window.called)
|
| 54 |
+
view.ok()
|
| 55 |
+
|
| 56 |
+
def test_init_nonmodal(self):
|
| 57 |
+
view = VW(root, 'Title', 'test text', modal=False)
|
| 58 |
+
self.assertFalse(VW.transient.called)
|
| 59 |
+
self.assertFalse(VW.grab_set.called)
|
| 60 |
+
self.assertFalse(VW.wait_window.called)
|
| 61 |
+
view.ok()
|
| 62 |
+
|
| 63 |
+
def test_ok(self):
|
| 64 |
+
view = VW(root, 'Title', 'test text', modal=False)
|
| 65 |
+
view.destroy = Func()
|
| 66 |
+
view.ok()
|
| 67 |
+
self.assertTrue(view.destroy.called)
|
| 68 |
+
del view.destroy # Unmask real function.
|
| 69 |
+
view.destroy()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class AutoHideScrollbarTest(unittest.TestCase):
|
| 73 |
+
# Method set is tested in ScrollableTextFrameTest
|
| 74 |
+
def test_forbidden_geometry(self):
|
| 75 |
+
scroll = tv.AutoHideScrollbar(root)
|
| 76 |
+
self.assertRaises(TclError, scroll.pack)
|
| 77 |
+
self.assertRaises(TclError, scroll.place)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class ScrollableTextFrameTest(unittest.TestCase):
|
| 81 |
+
|
| 82 |
+
@classmethod
|
| 83 |
+
def setUpClass(cls):
|
| 84 |
+
cls.root = root = Tk()
|
| 85 |
+
root.withdraw()
|
| 86 |
+
|
| 87 |
+
@classmethod
|
| 88 |
+
def tearDownClass(cls):
|
| 89 |
+
cls.root.update_idletasks()
|
| 90 |
+
cls.root.destroy()
|
| 91 |
+
del cls.root
|
| 92 |
+
|
| 93 |
+
def make_frame(self, wrap=NONE, **kwargs):
|
| 94 |
+
frame = tv.ScrollableTextFrame(self.root, wrap=wrap, **kwargs)
|
| 95 |
+
def cleanup_frame():
|
| 96 |
+
frame.update_idletasks()
|
| 97 |
+
frame.destroy()
|
| 98 |
+
self.addCleanup(cleanup_frame)
|
| 99 |
+
return frame
|
| 100 |
+
|
| 101 |
+
def test_line1(self):
|
| 102 |
+
frame = self.make_frame()
|
| 103 |
+
frame.text.insert('1.0', 'test text')
|
| 104 |
+
self.assertEqual(frame.text.get('1.0', '1.end'), 'test text')
|
| 105 |
+
|
| 106 |
+
def test_horiz_scrollbar(self):
|
| 107 |
+
# The horizontal scrollbar should be shown/hidden according to
|
| 108 |
+
# the 'wrap' setting: It should only be shown when 'wrap' is
|
| 109 |
+
# set to NONE.
|
| 110 |
+
|
| 111 |
+
# wrap = NONE -> with horizontal scrolling
|
| 112 |
+
frame = self.make_frame(wrap=NONE)
|
| 113 |
+
self.assertEqual(frame.text.cget('wrap'), NONE)
|
| 114 |
+
self.assertIsNotNone(frame.xscroll)
|
| 115 |
+
|
| 116 |
+
# wrap != NONE -> no horizontal scrolling
|
| 117 |
+
for wrap in [CHAR, WORD]:
|
| 118 |
+
with self.subTest(wrap=wrap):
|
| 119 |
+
frame = self.make_frame(wrap=wrap)
|
| 120 |
+
self.assertEqual(frame.text.cget('wrap'), wrap)
|
| 121 |
+
self.assertIsNone(frame.xscroll)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class ViewFrameTest(unittest.TestCase):
|
| 125 |
+
|
| 126 |
+
@classmethod
|
| 127 |
+
def setUpClass(cls):
|
| 128 |
+
cls.root = root = Tk()
|
| 129 |
+
root.withdraw()
|
| 130 |
+
cls.frame = tv.ViewFrame(root, 'test text')
|
| 131 |
+
|
| 132 |
+
@classmethod
|
| 133 |
+
def tearDownClass(cls):
|
| 134 |
+
del cls.frame
|
| 135 |
+
cls.root.update_idletasks()
|
| 136 |
+
cls.root.destroy()
|
| 137 |
+
del cls.root
|
| 138 |
+
|
| 139 |
+
def test_line1(self):
|
| 140 |
+
get = self.frame.text.get
|
| 141 |
+
self.assertEqual(get('1.0', '1.end'), 'test text')
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
# Call ViewWindow with modal=False.
|
| 145 |
+
class ViewFunctionTest(unittest.TestCase):
|
| 146 |
+
|
| 147 |
+
@classmethod
|
| 148 |
+
def setUpClass(cls):
|
| 149 |
+
cls.orig_error = tv.showerror
|
| 150 |
+
tv.showerror = Mbox_func()
|
| 151 |
+
|
| 152 |
+
@classmethod
|
| 153 |
+
def tearDownClass(cls):
|
| 154 |
+
tv.showerror = cls.orig_error
|
| 155 |
+
del cls.orig_error
|
| 156 |
+
|
| 157 |
+
def test_view_text(self):
|
| 158 |
+
view = tv.view_text(root, 'Title', 'test text', modal=False)
|
| 159 |
+
self.assertIsInstance(view, tv.ViewWindow)
|
| 160 |
+
self.assertIsInstance(view.viewframe, tv.ViewFrame)
|
| 161 |
+
view.viewframe.ok()
|
| 162 |
+
|
| 163 |
+
def test_view_file(self):
|
| 164 |
+
view = tv.view_file(root, 'Title', __file__, 'ascii', modal=False)
|
| 165 |
+
self.assertIsInstance(view, tv.ViewWindow)
|
| 166 |
+
self.assertIsInstance(view.viewframe, tv.ViewFrame)
|
| 167 |
+
get = view.viewframe.textframe.text.get
|
| 168 |
+
self.assertIn('Test', get('1.0', '1.end'))
|
| 169 |
+
view.ok()
|
| 170 |
+
|
| 171 |
+
def test_bad_file(self):
|
| 172 |
+
# Mock showerror will be used; view_file will return None.
|
| 173 |
+
view = tv.view_file(root, 'Title', 'abc.xyz', 'ascii', modal=False)
|
| 174 |
+
self.assertIsNone(view)
|
| 175 |
+
self.assertEqual(tv.showerror.title, 'File Load Error')
|
| 176 |
+
|
| 177 |
+
def test_bad_encoding(self):
|
| 178 |
+
p = os.path
|
| 179 |
+
fn = p.abspath(p.join(p.dirname(__file__), '..', 'CREDITS.txt'))
|
| 180 |
+
view = tv.view_file(root, 'Title', fn, 'ascii', modal=False)
|
| 181 |
+
self.assertIsNone(view)
|
| 182 |
+
self.assertEqual(tv.showerror.title, 'Unicode Decode Error')
|
| 183 |
+
|
| 184 |
+
def test_nowrap(self):
|
| 185 |
+
view = tv.view_text(root, 'Title', 'test', modal=False, wrap='none')
|
| 186 |
+
text_widget = view.viewframe.textframe.text
|
| 187 |
+
self.assertEqual(text_widget.cget('wrap'), 'none')
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# Call ViewWindow with _utest=True.
|
| 191 |
+
class ButtonClickTest(unittest.TestCase):
|
| 192 |
+
|
| 193 |
+
def setUp(self):
|
| 194 |
+
self.view = None
|
| 195 |
+
self.called = False
|
| 196 |
+
|
| 197 |
+
def tearDown(self):
|
| 198 |
+
if self.view:
|
| 199 |
+
self.view.destroy()
|
| 200 |
+
|
| 201 |
+
def test_view_text_bind_with_button(self):
|
| 202 |
+
def _command():
|
| 203 |
+
self.called = True
|
| 204 |
+
self.view = tv.view_text(root, 'TITLE_TEXT', 'COMMAND', _utest=True)
|
| 205 |
+
button = Button(root, text='BUTTON', command=_command)
|
| 206 |
+
button.invoke()
|
| 207 |
+
self.addCleanup(button.destroy)
|
| 208 |
+
|
| 209 |
+
self.assertEqual(self.called, True)
|
| 210 |
+
self.assertEqual(self.view.title(), 'TITLE_TEXT')
|
| 211 |
+
self.assertEqual(self.view.viewframe.textframe.text.get('1.0', '1.end'),
|
| 212 |
+
'COMMAND')
|
| 213 |
+
|
| 214 |
+
def test_view_file_bind_with_button(self):
|
| 215 |
+
def _command():
|
| 216 |
+
self.called = True
|
| 217 |
+
self.view = tv.view_file(root, 'TITLE_FILE', __file__,
|
| 218 |
+
encoding='ascii', _utest=True)
|
| 219 |
+
button = Button(root, text='BUTTON', command=_command)
|
| 220 |
+
button.invoke()
|
| 221 |
+
self.addCleanup(button.destroy)
|
| 222 |
+
|
| 223 |
+
self.assertEqual(self.called, True)
|
| 224 |
+
self.assertEqual(self.view.title(), 'TITLE_FILE')
|
| 225 |
+
get = self.view.viewframe.textframe.text.get
|
| 226 |
+
with open(__file__) as f:
|
| 227 |
+
self.assertEqual(get('1.0', '1.end'), f.readline().strip())
|
| 228 |
+
f.readline()
|
| 229 |
+
self.assertEqual(get('3.0', '3.end'), f.readline().strip())
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
if __name__ == '__main__':
|
| 233 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_tooltip.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test tooltip, coverage 100%.
|
| 2 |
+
|
| 3 |
+
Coverage is 100% after excluding 6 lines with "# pragma: no cover".
|
| 4 |
+
They involve TclErrors that either should or should not happen in a
|
| 5 |
+
particular situation, and which are 'pass'ed if they do.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from idlelib.tooltip import TooltipBase, Hovertip
|
| 9 |
+
from test.support import requires
|
| 10 |
+
requires('gui')
|
| 11 |
+
|
| 12 |
+
from functools import wraps
|
| 13 |
+
import time
|
| 14 |
+
from tkinter import Button, Tk, Toplevel
|
| 15 |
+
import unittest
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def setUpModule():
|
| 19 |
+
global root
|
| 20 |
+
root = Tk()
|
| 21 |
+
|
| 22 |
+
def tearDownModule():
|
| 23 |
+
global root
|
| 24 |
+
root.update_idletasks()
|
| 25 |
+
root.destroy()
|
| 26 |
+
del root
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def add_call_counting(func):
|
| 30 |
+
@wraps(func)
|
| 31 |
+
def wrapped_func(*args, **kwargs):
|
| 32 |
+
wrapped_func.call_args_list.append((args, kwargs))
|
| 33 |
+
return func(*args, **kwargs)
|
| 34 |
+
wrapped_func.call_args_list = []
|
| 35 |
+
return wrapped_func
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _make_top_and_button(testobj):
|
| 39 |
+
global root
|
| 40 |
+
top = Toplevel(root)
|
| 41 |
+
testobj.addCleanup(top.destroy)
|
| 42 |
+
top.title("Test tooltip")
|
| 43 |
+
button = Button(top, text='ToolTip test button')
|
| 44 |
+
button.pack()
|
| 45 |
+
testobj.addCleanup(button.destroy)
|
| 46 |
+
top.lift()
|
| 47 |
+
return top, button
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class ToolTipBaseTest(unittest.TestCase):
|
| 51 |
+
def setUp(self):
|
| 52 |
+
self.top, self.button = _make_top_and_button(self)
|
| 53 |
+
|
| 54 |
+
def test_base_class_is_unusable(self):
|
| 55 |
+
global root
|
| 56 |
+
top = Toplevel(root)
|
| 57 |
+
self.addCleanup(top.destroy)
|
| 58 |
+
|
| 59 |
+
button = Button(top, text='ToolTip test button')
|
| 60 |
+
button.pack()
|
| 61 |
+
self.addCleanup(button.destroy)
|
| 62 |
+
|
| 63 |
+
with self.assertRaises(NotImplementedError):
|
| 64 |
+
tooltip = TooltipBase(button)
|
| 65 |
+
tooltip.showtip()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class HovertipTest(unittest.TestCase):
|
| 69 |
+
def setUp(self):
|
| 70 |
+
self.top, self.button = _make_top_and_button(self)
|
| 71 |
+
|
| 72 |
+
def is_tipwindow_shown(self, tooltip):
|
| 73 |
+
return tooltip.tipwindow and tooltip.tipwindow.winfo_viewable()
|
| 74 |
+
|
| 75 |
+
def test_showtip(self):
|
| 76 |
+
tooltip = Hovertip(self.button, 'ToolTip text')
|
| 77 |
+
self.addCleanup(tooltip.hidetip)
|
| 78 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip))
|
| 79 |
+
tooltip.showtip()
|
| 80 |
+
self.assertTrue(self.is_tipwindow_shown(tooltip))
|
| 81 |
+
|
| 82 |
+
def test_showtip_twice(self):
|
| 83 |
+
tooltip = Hovertip(self.button, 'ToolTip text')
|
| 84 |
+
self.addCleanup(tooltip.hidetip)
|
| 85 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip))
|
| 86 |
+
tooltip.showtip()
|
| 87 |
+
self.assertTrue(self.is_tipwindow_shown(tooltip))
|
| 88 |
+
orig_tipwindow = tooltip.tipwindow
|
| 89 |
+
tooltip.showtip()
|
| 90 |
+
self.assertTrue(self.is_tipwindow_shown(tooltip))
|
| 91 |
+
self.assertIs(tooltip.tipwindow, orig_tipwindow)
|
| 92 |
+
|
| 93 |
+
def test_hidetip(self):
|
| 94 |
+
tooltip = Hovertip(self.button, 'ToolTip text')
|
| 95 |
+
self.addCleanup(tooltip.hidetip)
|
| 96 |
+
tooltip.showtip()
|
| 97 |
+
tooltip.hidetip()
|
| 98 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip))
|
| 99 |
+
|
| 100 |
+
def test_showtip_on_mouse_enter_no_delay(self):
|
| 101 |
+
tooltip = Hovertip(self.button, 'ToolTip text', hover_delay=None)
|
| 102 |
+
self.addCleanup(tooltip.hidetip)
|
| 103 |
+
tooltip.showtip = add_call_counting(tooltip.showtip)
|
| 104 |
+
root.update()
|
| 105 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip))
|
| 106 |
+
self.button.event_generate('<Enter>', x=0, y=0)
|
| 107 |
+
root.update()
|
| 108 |
+
self.assertTrue(self.is_tipwindow_shown(tooltip))
|
| 109 |
+
self.assertGreater(len(tooltip.showtip.call_args_list), 0)
|
| 110 |
+
|
| 111 |
+
def test_hover_with_delay(self):
|
| 112 |
+
# Run multiple tests requiring an actual delay simultaneously.
|
| 113 |
+
|
| 114 |
+
# Test #1: A hover tip with a non-zero delay appears after the delay.
|
| 115 |
+
tooltip1 = Hovertip(self.button, 'ToolTip text', hover_delay=100)
|
| 116 |
+
self.addCleanup(tooltip1.hidetip)
|
| 117 |
+
tooltip1.showtip = add_call_counting(tooltip1.showtip)
|
| 118 |
+
root.update()
|
| 119 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip1))
|
| 120 |
+
self.button.event_generate('<Enter>', x=0, y=0)
|
| 121 |
+
root.update()
|
| 122 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip1))
|
| 123 |
+
|
| 124 |
+
# Test #2: A hover tip with a non-zero delay doesn't appear when
|
| 125 |
+
# the mouse stops hovering over the base widget before the delay
|
| 126 |
+
# expires.
|
| 127 |
+
tooltip2 = Hovertip(self.button, 'ToolTip text', hover_delay=100)
|
| 128 |
+
self.addCleanup(tooltip2.hidetip)
|
| 129 |
+
tooltip2.showtip = add_call_counting(tooltip2.showtip)
|
| 130 |
+
root.update()
|
| 131 |
+
self.button.event_generate('<Enter>', x=0, y=0)
|
| 132 |
+
root.update()
|
| 133 |
+
self.button.event_generate('<Leave>', x=0, y=0)
|
| 134 |
+
root.update()
|
| 135 |
+
|
| 136 |
+
time.sleep(0.15)
|
| 137 |
+
root.update()
|
| 138 |
+
|
| 139 |
+
# Test #1 assertions.
|
| 140 |
+
self.assertTrue(self.is_tipwindow_shown(tooltip1))
|
| 141 |
+
self.assertGreater(len(tooltip1.showtip.call_args_list), 0)
|
| 142 |
+
|
| 143 |
+
# Test #2 assertions.
|
| 144 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip2))
|
| 145 |
+
self.assertEqual(tooltip2.showtip.call_args_list, [])
|
| 146 |
+
|
| 147 |
+
def test_hidetip_on_mouse_leave(self):
|
| 148 |
+
tooltip = Hovertip(self.button, 'ToolTip text', hover_delay=None)
|
| 149 |
+
self.addCleanup(tooltip.hidetip)
|
| 150 |
+
tooltip.showtip = add_call_counting(tooltip.showtip)
|
| 151 |
+
root.update()
|
| 152 |
+
self.button.event_generate('<Enter>', x=0, y=0)
|
| 153 |
+
root.update()
|
| 154 |
+
self.button.event_generate('<Leave>', x=0, y=0)
|
| 155 |
+
root.update()
|
| 156 |
+
self.assertFalse(self.is_tipwindow_shown(tooltip))
|
| 157 |
+
self.assertGreater(len(tooltip.showtip.call_args_list), 0)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
if __name__ == '__main__':
|
| 161 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_tree.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Test tree. coverage 56%."
|
| 2 |
+
|
| 3 |
+
from idlelib import tree
|
| 4 |
+
import unittest
|
| 5 |
+
from test.support import requires
|
| 6 |
+
requires('gui')
|
| 7 |
+
from tkinter import Tk, EventType, SCROLL
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TreeTest(unittest.TestCase):
|
| 11 |
+
|
| 12 |
+
@classmethod
|
| 13 |
+
def setUpClass(cls):
|
| 14 |
+
cls.root = Tk()
|
| 15 |
+
cls.root.withdraw()
|
| 16 |
+
|
| 17 |
+
@classmethod
|
| 18 |
+
def tearDownClass(cls):
|
| 19 |
+
cls.root.destroy()
|
| 20 |
+
del cls.root
|
| 21 |
+
|
| 22 |
+
def test_init(self):
|
| 23 |
+
# Start with code slightly adapted from htest.
|
| 24 |
+
sc = tree.ScrolledCanvas(
|
| 25 |
+
self.root, bg="white", highlightthickness=0, takefocus=1)
|
| 26 |
+
sc.frame.pack(expand=1, fill="both", side='left')
|
| 27 |
+
item = tree.FileTreeItem(tree.ICONDIR)
|
| 28 |
+
node = tree.TreeNode(sc.canvas, None, item)
|
| 29 |
+
node.expand()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class TestScrollEvent(unittest.TestCase):
|
| 33 |
+
|
| 34 |
+
def test_wheel_event(self):
|
| 35 |
+
# Fake widget class containing `yview` only.
|
| 36 |
+
class _Widget:
|
| 37 |
+
def __init__(widget, *expected):
|
| 38 |
+
widget.expected = expected
|
| 39 |
+
def yview(widget, *args):
|
| 40 |
+
self.assertTupleEqual(widget.expected, args)
|
| 41 |
+
# Fake event class
|
| 42 |
+
class _Event:
|
| 43 |
+
pass
|
| 44 |
+
# (type, delta, num, amount)
|
| 45 |
+
tests = ((EventType.MouseWheel, 120, -1, -5),
|
| 46 |
+
(EventType.MouseWheel, -120, -1, 5),
|
| 47 |
+
(EventType.ButtonPress, -1, 4, -5),
|
| 48 |
+
(EventType.ButtonPress, -1, 5, 5))
|
| 49 |
+
|
| 50 |
+
event = _Event()
|
| 51 |
+
for ty, delta, num, amount in tests:
|
| 52 |
+
event.type = ty
|
| 53 |
+
event.delta = delta
|
| 54 |
+
event.num = num
|
| 55 |
+
res = tree.wheel_event(event, _Widget(SCROLL, amount, "units"))
|
| 56 |
+
self.assertEqual(res, "break")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_util.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test util, coverage 100%"""
|
| 2 |
+
|
| 3 |
+
import unittest
|
| 4 |
+
from idlelib import util
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class UtilTest(unittest.TestCase):
|
| 8 |
+
def test_extensions(self):
|
| 9 |
+
for extension in {'.pyi', '.py', '.pyw'}:
|
| 10 |
+
self.assertIn(extension, util.py_extensions)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
if __name__ == '__main__':
|
| 14 |
+
unittest.main(verbosity=2)
|
llava/lib/python3.10/idlelib/idle_test/test_warning.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Test warnings replacement in pyshell.py and run.py.
|
| 2 |
+
|
| 3 |
+
This file could be expanded to include traceback overrides
|
| 4 |
+
(in same two modules). If so, change name.
|
| 5 |
+
Revise if output destination changes (http://bugs.python.org/issue18318).
|
| 6 |
+
Make sure warnings module is left unaltered (http://bugs.python.org/issue18081).
|
| 7 |
+
'''
|
| 8 |
+
from idlelib import run
|
| 9 |
+
from idlelib import pyshell as shell
|
| 10 |
+
import unittest
|
| 11 |
+
from test.support import captured_stderr
|
| 12 |
+
import warnings
|
| 13 |
+
|
| 14 |
+
# Try to capture default showwarning before Idle modules are imported.
|
| 15 |
+
showwarning = warnings.showwarning
|
| 16 |
+
# But if we run this file within idle, we are in the middle of the run.main loop
|
| 17 |
+
# and default showwarnings has already been replaced.
|
| 18 |
+
running_in_idle = 'idle' in showwarning.__name__
|
| 19 |
+
|
| 20 |
+
# The following was generated from pyshell.idle_formatwarning
|
| 21 |
+
# and checked as matching expectation.
|
| 22 |
+
idlemsg = '''
|
| 23 |
+
Warning (from warnings module):
|
| 24 |
+
File "test_warning.py", line 99
|
| 25 |
+
Line of code
|
| 26 |
+
UserWarning: Test
|
| 27 |
+
'''
|
| 28 |
+
shellmsg = idlemsg + ">>> "
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class RunWarnTest(unittest.TestCase):
|
| 32 |
+
|
| 33 |
+
@unittest.skipIf(running_in_idle, "Does not work when run within Idle.")
|
| 34 |
+
def test_showwarnings(self):
|
| 35 |
+
self.assertIs(warnings.showwarning, showwarning)
|
| 36 |
+
run.capture_warnings(True)
|
| 37 |
+
self.assertIs(warnings.showwarning, run.idle_showwarning_subproc)
|
| 38 |
+
run.capture_warnings(False)
|
| 39 |
+
self.assertIs(warnings.showwarning, showwarning)
|
| 40 |
+
|
| 41 |
+
def test_run_show(self):
|
| 42 |
+
with captured_stderr() as f:
|
| 43 |
+
run.idle_showwarning_subproc(
|
| 44 |
+
'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
|
| 45 |
+
# The following uses .splitlines to erase line-ending differences
|
| 46 |
+
self.assertEqual(idlemsg.splitlines(), f.getvalue().splitlines())
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class ShellWarnTest(unittest.TestCase):
|
| 50 |
+
|
| 51 |
+
@unittest.skipIf(running_in_idle, "Does not work when run within Idle.")
|
| 52 |
+
def test_showwarnings(self):
|
| 53 |
+
self.assertIs(warnings.showwarning, showwarning)
|
| 54 |
+
shell.capture_warnings(True)
|
| 55 |
+
self.assertIs(warnings.showwarning, shell.idle_showwarning)
|
| 56 |
+
shell.capture_warnings(False)
|
| 57 |
+
self.assertIs(warnings.showwarning, showwarning)
|
| 58 |
+
|
| 59 |
+
def test_idle_formatter(self):
|
| 60 |
+
# Will fail if format changed without regenerating idlemsg
|
| 61 |
+
s = shell.idle_formatwarning(
|
| 62 |
+
'Test', UserWarning, 'test_warning.py', 99, 'Line of code')
|
| 63 |
+
self.assertEqual(idlemsg, s)
|
| 64 |
+
|
| 65 |
+
def test_shell_show(self):
|
| 66 |
+
with captured_stderr() as f:
|
| 67 |
+
shell.idle_showwarning(
|
| 68 |
+
'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
|
| 69 |
+
self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines())
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
unittest.main(verbosity=2)
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_backward_compositeimplicitautograd_dispatch.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeimplicitautograd {
|
| 19 |
+
|
| 20 |
+
TORCH_API void _backward(const at::Tensor & self, at::TensorList inputs, const ::std::optional<at::Tensor> & gradient={}, ::std::optional<bool> retain_graph=::std::nullopt, bool create_graph=false);
|
| 21 |
+
|
| 22 |
+
} // namespace compositeimplicitautograd
|
| 23 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_fft_c2c_cuda_dispatch.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace cuda {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor _fft_c2c(const at::Tensor & self, at::IntArrayRef dim, int64_t normalization, bool forward);
|
| 21 |
+
TORCH_API at::Tensor _fft_c2c_symint(const at::Tensor & self, c10::SymIntArrayRef dim, int64_t normalization, bool forward);
|
| 22 |
+
TORCH_API at::Tensor & _fft_c2c_out(at::Tensor & out, const at::Tensor & self, at::IntArrayRef dim, int64_t normalization, bool forward);
|
| 23 |
+
TORCH_API at::Tensor & _fft_c2c_outf(const at::Tensor & self, at::IntArrayRef dim, int64_t normalization, bool forward, at::Tensor & out);
|
| 24 |
+
TORCH_API at::Tensor & _fft_c2c_symint_out(at::Tensor & out, const at::Tensor & self, c10::SymIntArrayRef dim, int64_t normalization, bool forward);
|
| 25 |
+
TORCH_API at::Tensor & _fft_c2c_symint_outf(const at::Tensor & self, c10::SymIntArrayRef dim, int64_t normalization, bool forward, at::Tensor & out);
|
| 26 |
+
|
| 27 |
+
} // namespace cuda
|
| 28 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_asin_cuda_dispatch.h
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace cuda {
|
| 19 |
+
|
| 20 |
+
TORCH_API ::std::vector<at::Tensor> _foreach_asin(at::TensorList self);
|
| 21 |
+
TORCH_API void _foreach_asin_(at::TensorList self);
|
| 22 |
+
|
| 23 |
+
} // namespace cuda
|
| 24 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_lgamma.h
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/_foreach_lgamma_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::_foreach_lgamma(Tensor[] self) -> Tensor[]
|
| 26 |
+
inline ::std::vector<at::Tensor> _foreach_lgamma(at::TensorList self) {
|
| 27 |
+
return at::_ops::_foreach_lgamma::call(self);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// aten::_foreach_lgamma_(Tensor(a!)[] self) -> ()
|
| 31 |
+
inline void _foreach_lgamma_(at::TensorList self) {
|
| 32 |
+
return at::_ops::_foreach_lgamma_::call(self);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// aten::_foreach_lgamma.out(Tensor[] self, *, Tensor(a!)[] out) -> ()
|
| 36 |
+
inline void _foreach_lgamma_out(at::TensorList out, at::TensorList self) {
|
| 37 |
+
return at::_ops::_foreach_lgamma_out::call(self, out);
|
| 38 |
+
}
|
| 39 |
+
// aten::_foreach_lgamma.out(Tensor[] self, *, Tensor(a!)[] out) -> ()
|
| 40 |
+
inline void _foreach_lgamma_outf(at::TensorList self, at::TensorList out) {
|
| 41 |
+
return at::_ops::_foreach_lgamma_out::call(self, out);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
}
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_pow_compositeexplicitautograd_dispatch.h
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeexplicitautograd {
|
| 19 |
+
|
| 20 |
+
TORCH_API ::std::vector<at::Tensor> _foreach_pow(at::TensorList self, at::TensorList exponent);
|
| 21 |
+
TORCH_API void _foreach_pow_out(at::TensorList out, at::TensorList self, at::TensorList exponent);
|
| 22 |
+
TORCH_API void _foreach_pow_outf(at::TensorList self, at::TensorList exponent, at::TensorList out);
|
| 23 |
+
TORCH_API void _foreach_pow_(at::TensorList self, at::TensorList exponent);
|
| 24 |
+
TORCH_API ::std::vector<at::Tensor> _foreach_pow(at::TensorList self, const at::Scalar & exponent);
|
| 25 |
+
TORCH_API void _foreach_pow_out(at::TensorList out, at::TensorList self, const at::Scalar & exponent);
|
| 26 |
+
TORCH_API void _foreach_pow_outf(at::TensorList self, const at::Scalar & exponent, at::TensorList out);
|
| 27 |
+
TORCH_API void _foreach_pow_(at::TensorList self, const at::Scalar & exponent);
|
| 28 |
+
TORCH_API ::std::vector<at::Tensor> _foreach_pow(at::TensorList self, at::ArrayRef<at::Scalar> exponent);
|
| 29 |
+
TORCH_API void _foreach_pow_out(at::TensorList out, at::TensorList self, at::ArrayRef<at::Scalar> exponent);
|
| 30 |
+
TORCH_API void _foreach_pow_outf(at::TensorList self, at::ArrayRef<at::Scalar> exponent, at::TensorList out);
|
| 31 |
+
TORCH_API void _foreach_pow_(at::TensorList self, at::ArrayRef<at::Scalar> exponent);
|
| 32 |
+
TORCH_API ::std::vector<at::Tensor> _foreach_pow(const at::Scalar & self, at::TensorList exponent);
|
| 33 |
+
|
| 34 |
+
} // namespace compositeexplicitautograd
|
| 35 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_remove_batch_dim_compositeimplicitautograd_dispatch.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
// @generated by torchgen/gen.py from DispatchKeyFunction.h
|
| 3 |
+
|
| 4 |
+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
|
| 5 |
+
|
| 6 |
+
// The only #includes we need are for custom classes that have defaults in the C++ API
|
| 7 |
+
#include <c10/core/MemoryFormat.h>
|
| 8 |
+
#include <c10/core/Scalar.h>
|
| 9 |
+
#include <ATen/core/Reduction.h>
|
| 10 |
+
|
| 11 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 12 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 13 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 14 |
+
#include <ATen/core/ATen_fwd.h>
|
| 15 |
+
|
| 16 |
+
namespace at {
|
| 17 |
+
|
| 18 |
+
namespace compositeimplicitautograd {
|
| 19 |
+
|
| 20 |
+
TORCH_API at::Tensor _remove_batch_dim(const at::Tensor & self, int64_t level, int64_t batch_size, int64_t out_dim);
|
| 21 |
+
|
| 22 |
+
} // namespace compositeimplicitautograd
|
| 23 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_sparse_addmm_native.h
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from NativeFunction.h
|
| 4 |
+
|
| 5 |
+
#include <c10/core/Scalar.h>
|
| 6 |
+
#include <c10/core/Storage.h>
|
| 7 |
+
#include <c10/core/TensorOptions.h>
|
| 8 |
+
#include <c10/util/Deprecated.h>
|
| 9 |
+
#include <c10/util/Optional.h>
|
| 10 |
+
#include <c10/core/QScheme.h>
|
| 11 |
+
#include <ATen/core/Reduction.h>
|
| 12 |
+
#include <ATen/core/Tensor.h>
|
| 13 |
+
#include <tuple>
|
| 14 |
+
#include <vector>
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
namespace native {
|
| 19 |
+
TORCH_API at::Tensor _sparse_addmm(const at::Tensor & self, const at::Tensor & mat1, const at::Tensor & mat2, const at::Scalar & beta=1, const at::Scalar & alpha=1);
|
| 20 |
+
TORCH_API at::Tensor & _sparse_addmm_out(const at::Tensor & self, const at::Tensor & mat1, const at::Tensor & mat2, const at::Scalar & beta, const at::Scalar & alpha, at::Tensor & out);
|
| 21 |
+
} // namespace native
|
| 22 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_sparse_softmax_ops.h
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Operator.h
|
| 4 |
+
|
| 5 |
+
#include <tuple>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
// Forward declarations of any types needed in the operator signatures.
|
| 9 |
+
// We can't directly include these classes because it will cause circular include dependencies.
|
| 10 |
+
// This file is included by TensorBody.h, which defines the Tensor class.
|
| 11 |
+
#include <ATen/core/ATen_fwd.h>
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
namespace _ops {
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
struct TORCH_API _sparse_softmax_int {
|
| 18 |
+
using schema = at::Tensor (const at::Tensor &, int64_t, ::std::optional<at::ScalarType>);
|
| 19 |
+
using ptr_schema = schema*;
|
| 20 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 21 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_sparse_softmax")
|
| 22 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "int")
|
| 23 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_sparse_softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor")
|
| 24 |
+
static at::Tensor call(const at::Tensor & self, int64_t dim, ::std::optional<at::ScalarType> dtype);
|
| 25 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t dim, ::std::optional<at::ScalarType> dtype);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
struct TORCH_API _sparse_softmax_Dimname {
|
| 29 |
+
using schema = at::Tensor (const at::Tensor &, at::Dimname, ::std::optional<at::ScalarType>);
|
| 30 |
+
using ptr_schema = schema*;
|
| 31 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 32 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_sparse_softmax")
|
| 33 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "Dimname")
|
| 34 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_sparse_softmax.Dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor")
|
| 35 |
+
static at::Tensor call(const at::Tensor & self, at::Dimname dim, ::std::optional<at::ScalarType> dtype);
|
| 36 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, at::Dimname dim, ::std::optional<at::ScalarType> dtype);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
struct TORCH_API _sparse_softmax {
|
| 40 |
+
using schema = at::Tensor (const at::Tensor &, int64_t, bool);
|
| 41 |
+
using ptr_schema = schema*;
|
| 42 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 43 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_sparse_softmax")
|
| 44 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
|
| 45 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_sparse_softmax(Tensor self, int dim, bool half_to_float) -> Tensor")
|
| 46 |
+
static at::Tensor call(const at::Tensor & self, int64_t dim, bool half_to_float);
|
| 47 |
+
static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t dim, bool half_to_float);
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
struct TORCH_API _sparse_softmax_out {
|
| 51 |
+
using schema = at::Tensor & (const at::Tensor &, int64_t, bool, at::Tensor &);
|
| 52 |
+
using ptr_schema = schema*;
|
| 53 |
+
// See Note [static constexpr char* members for windows NVCC]
|
| 54 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_sparse_softmax")
|
| 55 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
|
| 56 |
+
STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_sparse_softmax.out(Tensor self, int dim, bool half_to_float, *, Tensor(a!) out) -> Tensor(a!)")
|
| 57 |
+
static at::Tensor & call(const at::Tensor & self, int64_t dim, bool half_to_float, at::Tensor & out);
|
| 58 |
+
static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t dim, bool half_to_float, at::Tensor & out);
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
}} // namespace at::_ops
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_upsample_nearest_exact1d_backward_native.h
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from NativeFunction.h
|
| 4 |
+
|
| 5 |
+
#include <c10/core/Scalar.h>
|
| 6 |
+
#include <c10/core/Storage.h>
|
| 7 |
+
#include <c10/core/TensorOptions.h>
|
| 8 |
+
#include <c10/util/Deprecated.h>
|
| 9 |
+
#include <c10/util/Optional.h>
|
| 10 |
+
#include <c10/core/QScheme.h>
|
| 11 |
+
#include <ATen/core/Reduction.h>
|
| 12 |
+
#include <ATen/core/Tensor.h>
|
| 13 |
+
#include <tuple>
|
| 14 |
+
#include <vector>
|
| 15 |
+
#include <ATen/ops/_upsample_nearest_exact1d_backward_meta.h>
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
namespace native {
|
| 19 |
+
struct TORCH_API structured__upsample_nearest_exact1d_backward_out_cpu : public at::meta::structured__upsample_nearest_exact1d_backward {
|
| 20 |
+
void impl(const at::Tensor & grad_output, at::ArrayRef<int64_t> output_size, at::ArrayRef<int64_t> input_size, ::std::optional<double> scales, const at::Tensor & grad_input);
|
| 21 |
+
};
|
| 22 |
+
struct TORCH_API structured__upsample_nearest_exact1d_backward_out_cuda : public at::meta::structured__upsample_nearest_exact1d_backward {
|
| 23 |
+
void impl(const at::Tensor & grad_output, at::ArrayRef<int64_t> output_size, at::ArrayRef<int64_t> input_size, ::std::optional<double> scales, const at::Tensor & grad_input);
|
| 24 |
+
};
|
| 25 |
+
} // namespace native
|
| 26 |
+
} // namespace at
|
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_values_copy.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// @generated by torchgen/gen.py from Function.h
|
| 4 |
+
|
| 5 |
+
#include <ATen/Context.h>
|
| 6 |
+
#include <ATen/DeviceGuard.h>
|
| 7 |
+
#include <ATen/TensorUtils.h>
|
| 8 |
+
#include <ATen/TracerMode.h>
|
| 9 |
+
#include <ATen/core/Generator.h>
|
| 10 |
+
#include <ATen/core/Reduction.h>
|
| 11 |
+
#include <ATen/core/Tensor.h>
|
| 12 |
+
#include <c10/core/Scalar.h>
|
| 13 |
+
#include <c10/core/Storage.h>
|
| 14 |
+
#include <c10/core/TensorOptions.h>
|
| 15 |
+
#include <c10/util/Deprecated.h>
|
| 16 |
+
#include <c10/util/Optional.h>
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
#include <ATen/ops/_values_copy_ops.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
// aten::_values_copy(Tensor self) -> Tensor
|
| 26 |
+
inline at::Tensor _values_copy(const at::Tensor & self) {
|
| 27 |
+
return at::_ops::_values_copy::call(self);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// aten::_values_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
|
| 31 |
+
inline at::Tensor & _values_copy_out(at::Tensor & out, const at::Tensor & self) {
|
| 32 |
+
return at::_ops::_values_copy_out::call(self, out);
|
| 33 |
+
}
|
| 34 |
+
// aten::_values_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
|
| 35 |
+
inline at::Tensor & _values_copy_outf(const at::Tensor & self, at::Tensor & out) {
|
| 36 |
+
return at::_ops::_values_copy_out::call(self, out);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
}
|