Debian Codegraph @

test/test_inference/test_namespace_package.py

raw symbols SCIP
1 from os.path import join
2
3 import pytest
4 import py
5
6 from ..helpers import get_example_dir, example_dir
7 from jedi import Project
8
9
10 SYS_PATH = [get_example_dir('namespace_package', 'ns1'),
11 get_example_dir('namespace_package', 'ns2')]
12
13
14 def script_with_path(Script, *args, **kwargs):
15 return Script(project=Project('.', sys_path=SYS_PATH), *args, **kwargs)
16
17
18 def test_goto_definition(Script):
19 assert script_with_path(Script, 'from pkg import ns1_file').infer()
20 assert script_with_path(Script, 'from pkg import ns2_file').infer()
21 assert not script_with_path(Script, 'from pkg import ns3_file').infer()
22
23
24 @pytest.mark.parametrize(
25 ('source', 'solution'), [
26 ('from pkg.ns2_folder.nested import foo', 'nested!'),
27 ('from pkg.ns2_folder import foo', 'ns2_folder!'),
28 ('from pkg.ns2_file import foo', 'ns2_file!'),
29 ('from pkg.ns1_folder import foo', 'ns1_folder!'),
30 ('from pkg.ns1_file import foo', 'ns1_file!'),
31 ('from pkg import foo', 'ns1!'),
32 ]
33 )
34 def test_goto_assignment(Script, source, solution):
35 ass = script_with_path(Script, source).goto()
36 assert len(ass) == 1
37 assert ass[0].description == "foo = '%s'" % solution
38
39
40 def test_simple_completions(Script):
41 # completion
42 completions = script_with_path(Script, 'from pkg import ').complete()
43 names = [c.name for c in completions]
44 compare = ['foo', 'ns1_file', 'ns1_folder', 'ns2_folder', 'ns2_file',
45 'pkg_resources', 'pkgutil', '__name__', '__path__',
46 '__package__', '__file__', '__doc__']
47 # must at least contain these items, other items are not important
48 assert set(compare) == set(names)
49
50
51 @pytest.mark.parametrize(
52 ('source', 'solution'), [
53 ('from pkg import ns2_folder as x', 'ns2_folder!'),
54 ('from pkg import ns2_file as x', 'ns2_file!'),
55 ('from pkg.ns2_folder import nested as x', 'nested!'),
56 ('from pkg import ns1_folder as x', 'ns1_folder!'),
57 ('from pkg import ns1_file as x', 'ns1_file!'),
58 ('import pkg as x', 'ns1!'),
59 ]
60 )
61 def test_completions(Script, source, solution):
62 for c in script_with_path(Script, source + '; x.').complete():
63 if c.name == 'foo':
64 completion = c
65 solution = "foo = '%s'" % solution
66 assert completion.description == solution
67
68
69 def test_nested_namespace_package(Script):
70 code = 'from nested_namespaces.namespace.pkg import CONST'
71
72 sys_path = [example_dir]
73 project = Project('.', sys_path=sys_path)
74 result = Script(code, project=project).infer(line=1, column=45)
75
76 assert len(result) == 1
77
78
79 def test_relative_import(Script, environment, tmpdir):
80 """
81 Attempt a relative import in a very simple namespace package.
82 """
83 directory = get_example_dir('namespace_package_relative_import')
84 # Need to copy the content in a directory where there's no __init__.py.
85 py.path.local(directory).copy(tmpdir)
86 file_path = join(tmpdir.strpath, "rel1.py")
87 script = Script(path=file_path)
88 d, = script.infer(line=1)
89 assert d.name == 'int'
90 d, = script.goto(line=1)
91 assert d.name == 'name'
92 assert d.module_name == 'rel2'