Debian Codegraph @

test/test_utils.py

raw symbols SCIP
1 import sys
2 from typing import Any
3
4 try:
5 import readline
6 except ImportError:
7 readline = False # type: ignore[assignment]
8 import unittest
9
10 from jedi import utils
11
12
13 @unittest.skipIf(not readline, "readline not found")
14 class TestSetupReadline(unittest.TestCase):
15 class NameSpace(object):
16 pass
17
18 def setUp(self, *args, **kwargs):
19 super().setUp(*args, **kwargs)
20
21 self.namespace: Any = self.NameSpace()
22 utils.setup_readline(self.namespace)
23
24 def complete(self, text):
25 completer = readline.get_completer()
26 i = 0
27 completions = []
28 while True:
29 completion = completer(text, i)
30 if completion is None:
31 break
32 completions.append(completion)
33 i += 1
34 return completions
35
36 def test_simple(self):
37 assert self.complete('list') == ['list']
38 assert self.complete('importerror') == ['ImportError']
39 s = "print(BaseE"
40 if sys.version_info >= (3, 11):
41 assert self.complete(s) == [s + 'xception', s + 'xceptionGroup']
42 else:
43 assert self.complete(s) == [s + 'xception']
44
45 def test_nested(self):
46 assert self.complete('list.Insert') == ['list.insert']
47 assert self.complete('list().Insert') == ['list().insert']
48
49 def test_magic_methods(self):
50 assert self.complete('list.__getitem__') == ['list.__getitem__']
51 assert self.complete('list().__getitem__') == ['list().__getitem__']
52
53 def test_modules(self):
54 import sys
55 import os
56 self.namespace.sys = sys # type: ignore[attr-defined]
57 self.namespace.os = os # type: ignore[attr-defined]
58
59 try:
60 assert self.complete('os.path.join') == ['os.path.join']
61 string = 'os.path.join("a").upper'
62 assert self.complete(string) == [string]
63
64 c = {'os.' + d for d in dir(os) if d.startswith('ch')}
65 assert set(self.complete('os.ch')) == set(c)
66 finally:
67 del self.namespace.sys # type: ignore[attr-defined]
68 del self.namespace.os # type: ignore[attr-defined]
69
70 def test_calls(self):
71 s = 'str(bytes'
72 assert self.complete(s) == [s, 'str(BytesWarning']
73
74 def test_import(self):
75 s = 'from os.path import a'
76 assert set(self.complete(s)) == {
77 s + 'ltsep',
78 s + 'bspath',
79 'from os.path import ALLOW_MISSING'
80 }
81 assert self.complete('import keyword') == ['import keyword']
82
83 import os
84 s = 'from os import '
85 goal = {s + el for el in dir(os)}
86
87 # There are minor differences, e.g. the dir doesn't include deleted
88 # items as well as items that are not only available on linux.
89 difference = set(self.complete(s)).symmetric_difference(goal)
90 ACCEPTED_DIFFERENCE_PREFIXES = [
91 '_', 'O_', 'EX_', 'EFD_', 'MFD_', 'TFD_',
92 'SF_', 'ST_', 'CLD_', 'POSIX_SPAWN_', 'P_',
93 'RWF_', 'CLONE_', 'SCHED_', 'SPLICE_',
94 # Python 3.15+ new constants
95 'AT_', 'PIDFD_', 'STATX_', 'GRND_', 'XATTR_',
96 'RTLD_', 'PRIO_', 'F_', 'SEEK_', 'NODEV',
97 ]
98 difference = {
99 x for x in difference
100 if not any(
101 x.startswith('from os import ' + prefix)
102 for prefix in ACCEPTED_DIFFERENCE_PREFIXES
103 )
104 }
105 # There are quite a few differences, because both Windows and Linux
106 # (posix and nt) libraries are included.
107 assert len(difference) < 40
108
109 def test_local_import(self):
110 s = 'import test.test_utils'
111 assert self.complete(s) == [s]
112
113 def test_preexisting_values(self):
114 self.namespace.a = range(10)
115 assert set(self.complete('a.')) == {'a.' + n for n in dir(range(1))}
116 del self.namespace.a
117
118 def test_colorama(self):
119 """
120 Only test it if colorama library is available.
121
122 This module is being tested because it uses ``setattr`` at some point,
123 which Jedi doesn't understand, but it should still work in the REPL.
124 """
125 try:
126 # if colorama is installed
127 import colorama
128 except ImportError:
129 pass
130 else:
131 self.namespace.colorama = colorama
132 assert self.complete('colorama')
133 assert self.complete('colorama.Fore.BLACK') == ['colorama.Fore.BLACK']
134 del self.namespace.colorama
135
136
137 def test_version_info():
138 assert utils.version_info()[:2] > (0, 7)