99import sys
1010import textwrap
1111import unittest
12+ import random
1213
1314from importlib .metadata import version
1415
15-
1616from contextlib import contextmanager
1717
1818from traitlets .config .loader import Config
2121from IPython .utils .tempdir import TemporaryDirectory , TemporaryWorkingDirectory
2222from IPython .utils .generics import complete_object
2323from IPython .testing import decorators as dec
24+ from IPython .core .latex_symbols import latex_symbols
2425
2526from IPython .core .completer import (
2627 Completion ,
3132 completion_matcher ,
3233 SimpleCompletion ,
3334 CompletionContext ,
35+ _unicode_name_compute ,
36+ _UNICODE_RANGES ,
3437)
3538
3639from packaging .version import parse
3740
3841
42+ @contextmanager
43+ def jedi_status (status : bool ):
44+ completer = get_ipython ().Completer
45+ try :
46+ old = completer .use_jedi
47+ completer .use_jedi = status
48+ yield
49+ finally :
50+ completer .use_jedi = old
51+
52+
3953# -----------------------------------------------------------------------------
4054# Test functions
4155# -----------------------------------------------------------------------------
@@ -66,7 +80,7 @@ def ranges(i):
6680 rg = list (ranges (valid ))
6781 lens = []
6882 gap_lens = []
69- pstart , pstop = 0 , 0
83+ _pstart , pstop = 0 , 0
7084 for start , stop in rg :
7185 lens .append (stop - start )
7286 gap_lens .append (
@@ -77,7 +91,7 @@ def ranges(i):
7791 f"{ round ((start - pstop )/ 0xe01f0 * 100 )} %" ,
7892 )
7993 )
80- pstart , pstop = start , stop
94+ _pstart , pstop = start , stop
8195
8296 return sorted (gap_lens )[- 1 ]
8397
@@ -87,7 +101,6 @@ def test_unicode_range():
87101 Test that the ranges we test for unicode names give the same number of
88102 results than testing the full length.
89103 """
90- from IPython .core .completer import _unicode_name_compute , _UNICODE_RANGES
91104
92105 expected_list = _unicode_name_compute ([(0 , 0x110000 )])
93106 test = _unicode_name_compute (_UNICODE_RANGES )
@@ -148,45 +161,45 @@ def custom_matchers(matchers):
148161 ip .Completer .custom_matchers .clear ()
149162
150163
151- def test_protect_filename () :
152- if sys . platform == "win32" :
153- pairs = [
154- ( " abc" , " abc" ),
155- ( " abc " , '" abc "' ),
156- ("a bc" , '"a bc"' ),
157- ( "a bc" , '"a bc"' ),
158- ( " bc" , '" bc"' ),
159- ]
160- else :
161- pairs = [
162- ( " abc" , " abc" ),
163- ( " abc " , r"\ abc " ),
164- ("a bc" , r"a\ bc" ),
165- ( "a bc" , r"a \ \ bc" ),
166- ( " bc" , r"\ \ bc" ),
167- # On posix, we also protect parens and other special characters.
168- ("a( bc" , r"a\( bc" ),
169- ("a)bc" , r"a\)bc" ),
170- ("a( ) bc" , r"a\(\ \) bc" ),
171- ("a[1] bc" , r"a\[1\] bc" ),
172- ("a{1} bc" , r"a\{1\} bc" ),
173- ("a# bc" , r"a\# bc" ),
174- ("a? bc" , r"a\? bc" ),
175- ("a= bc" , r"a\= bc" ),
176- ("a\\ bc" , r"a\\ bc" ),
177- ("a| bc" , r"a\| bc" ),
178- ("a; bc" , r"a\; bc" ),
179- ("a: bc" , r"a\: bc" ),
180- ("a' bc" , r"a\' bc" ),
181- ( "a*bc" , r"a\* bc" ),
182- ( 'a"bc' , r"a\" bc" ),
183- ("a^ bc" , r"a\^ bc" ),
184- ( "a&bc" , r"a\&bc" ),
185- ]
186- # run the actual tests
187- for s1 , s2 in pairs :
188- s1p = completer . protect_filename (s1 )
189- assert s1p == s2
164+ if sys . platform == "win32" :
165+ pairs = [
166+ ( "abc" , "abc" ),
167+ ( " abc" , '" abc"' ),
168+ ( "a bc " , '"a bc "' ),
169+ ("a bc" , '"a bc"' ),
170+ ( " bc" , '" bc"' ),
171+ ]
172+ else :
173+ pairs = [
174+ ( "abc" , "abc" ),
175+ ( " abc" , r"\ abc" ),
176+ ( "a bc " , r"a\ bc " ),
177+ ("a bc" , r"a\ \ bc" ),
178+ ( " bc" , r"\ \ bc" ),
179+ # On posix, we also protect parens and other special characters.
180+ ( "a(bc" , r"a\(bc" ),
181+ ("a) bc" , r"a\) bc" ),
182+ ("a( )bc" , r"a\(\ \)bc" ),
183+ ("a[1] bc" , r"a\[1\] bc" ),
184+ ("a{1} bc" , r"a\{1\} bc" ),
185+ ("a# bc" , r"a\# bc" ),
186+ ("a? bc" , r"a\? bc" ),
187+ ("a= bc" , r"a\= bc" ),
188+ ("a\\ bc" , r"a\\ bc" ),
189+ ("a| bc" , r"a\| bc" ),
190+ ("a; bc" , r"a\; bc" ),
191+ ("a: bc" , r"a\: bc" ),
192+ ("a' bc" , r"a\' bc" ),
193+ ("a* bc" , r"a\* bc" ),
194+ ( 'a"bc' , r"a\" bc" ),
195+ ( "a^bc" , r"a\^ bc" ),
196+ ("a& bc" , r"a\& bc" ),
197+ ]
198+
199+
200+ @ pytest . mark . parametrize ( " s1,expected" , pairs )
201+ def test_protect_filename (s1 , expected ):
202+ assert completer . protect_filename ( s1 ) == expected
190203
191204
192205def check_line_split (splitter , test_specs ):
@@ -297,8 +310,6 @@ def test_unicode_completions(self):
297310 self .assertIsInstance (matches , list )
298311
299312 def test_latex_completions (self ):
300- from IPython .core .latex_symbols import latex_symbols
301- import random
302313
303314 ip = get_ipython ()
304315 # Test some random unicode symbols
@@ -1734,6 +1745,45 @@ def _(expected):
17341745 _ (["completion_a" ])
17351746
17361747
1748+ @pytest .mark .parametrize (
1749+ "setup,code,expected,not_expected" ,
1750+ [
1751+ ('a="str"; b=1' , "(a, b." , [".bit_count" , ".conjugate" ], [".count" ]),
1752+ ('a="str"; b=1' , "(a, b)." , [".count" ], [".bit_count" , ".capitalize" ]),
1753+ ('x="str"; y=1' , "x = {1, y." , [".bit_count" ], [".count" ]),
1754+ ('x="str"; y=1' , "x = [1, y." , [".bit_count" ], [".count" ]),
1755+ ('x="str"; y=1; fun=lambda x:x' , "x = fun(1, y." , [".bit_count" ], [".count" ]),
1756+ ],
1757+ )
1758+ def test_misc_no_jedi_completions (setup , code , expected , not_expected ):
1759+ ip = get_ipython ()
1760+ c = ip .Completer
1761+ ip .ex (setup )
1762+ with provisionalcompleter (), jedi_status (False ):
1763+ matches = c .all_completions (code )
1764+ assert set (expected ) - set (matches ) == set (), set (matches )
1765+ assert set (matches ).intersection (set (not_expected )) == set ()
1766+
1767+
1768+ @pytest .mark .parametrize (
1769+ "code,expected" ,
1770+ [
1771+ (" (a, b" , "b" ),
1772+ ("(a, b" , "b" ),
1773+ ("(a, b)" , "" ), # trim always start by trimming
1774+ (" (a, b)" , "(a, b)" ),
1775+ (" [a, b]" , "[a, b]" ),
1776+ (" a, b" , "b" ),
1777+ ("x = {1, y" , "y" ),
1778+ ("x = [1, y" , "y" ),
1779+ ("x = fun(1, y" , "y" ),
1780+ ],
1781+ )
1782+ def test_trim_expr (code , expected ):
1783+ c = get_ipython ().Completer
1784+ assert c ._trim_expr (code ) == expected
1785+
1786+
17371787@pytest .mark .parametrize (
17381788 "input, expected" ,
17391789 [
0 commit comments