Debian Codegraph @

test/test_inference/test_implicit_namespace_package.py

raw symbols SCIP
1 from test.helpers import get_example_dir, example_dir
2 from jedi import Project
3
4
5 def test_implicit_namespace_package(Script):
6 sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
7 get_example_dir('implicit_namespace_package', 'ns2')]
8 project = Project('.', sys_path=sys_path)
9
10 def script_with_path(*args, **kwargs):
11 return Script(project=project, *args, **kwargs)
12
13 # goto definition
14 assert script_with_path('from pkg import ns1_file').infer()
15 assert script_with_path('from pkg import ns2_file').infer()
16 assert not script_with_path('from pkg import ns3_file').infer()
17
18 # goto assignment
19 tests = {
20 'from pkg.ns2_file import foo': 'ns2_file!',
21 'from pkg.ns1_file import foo': 'ns1_file!',
22 }
23 for source, solution in tests.items():
24 ass = script_with_path(source).goto()
25 assert len(ass) == 1
26 assert ass[0].description == "foo = '%s'" % solution
27
28 # completion
29 completions = script_with_path('from pkg import ').complete()
30 names = [c.name for c in completions]
31 compare = ['ns1_file', 'ns2_file']
32 # must at least contain these items, other items are not important
33 assert set(compare) == set(names)
34
35 tests = {
36 'from pkg import ns2_file as x': 'ns2_file!',
37 'from pkg import ns1_file as x': 'ns1_file!'
38 }
39 for source, solution in tests.items():
40 for c in script_with_path(source + '; x.').complete():
41 if c.name == 'foo':
42 completion = c
43 solution = "foo = '%s'" % solution
44 assert completion.description == solution
45
46 c, = script_with_path('import pkg').complete()
47 assert c.docstring() == ""
48
49
50 def test_implicit_nested_namespace_package(Script):
51 code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
52
53 project = Project('.', sys_path=[example_dir])
54 script = Script(code, project=project)
55
56 result = script.infer(line=1, column=61)
57
58 assert len(result) == 1
59
60 implicit_pkg, = Script(code, project=project).infer(column=10)
61 assert implicit_pkg.type == 'namespace'
62 assert implicit_pkg.module_path is None
63
64
65 def test_implicit_namespace_package_import_autocomplete(Script):
66 code = 'from implicit_name'
67
68 project = Project('.', sys_path=[example_dir])
69 script = Script(code, project=project)
70 compl = script.complete()
71 assert [c.name for c in compl] == ['implicit_namespace_package']
72
73
74 def test_namespace_package_in_multiple_directories_autocompletion(Script):
75 code = 'from pkg.'
76 sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
77 get_example_dir('implicit_namespace_package', 'ns2')]
78
79 project = Project('.', sys_path=sys_path)
80 script = Script(code, project=project)
81 compl = script.complete()
82 assert set(c.name for c in compl) == set(['ns1_file', 'ns2_file'])
83
84
85 def test_namespace_package_in_multiple_directories_goto_definition(Script):
86 code = 'from pkg import ns1_file'
87 sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
88 get_example_dir('implicit_namespace_package', 'ns2')]
89 project = Project('.', sys_path=sys_path)
90 script = Script(code, project=project)
91 result = script.infer()
92 assert len(result) == 1
93
94
95 def test_namespace_name_autocompletion_full_name(Script):
96 code = 'from pk'
97 sys_path = [get_example_dir('implicit_namespace_package', 'ns1'),
98 get_example_dir('implicit_namespace_package', 'ns2')]
99
100 project = Project('.', sys_path=sys_path)
101 script = Script(code, project=project)
102 compl = script.complete()
103 assert set(c.full_name for c in compl) == set(['pkg'])