| 1 |
""" |
| 2 |
Tests for :attr:`.BaseName.full_name`. |
| 3 |
|
| 4 |
There are three kinds of test: |
| 5 |
|
| 6 |
#. Test classes derived from :class:`MixinTestFullName`. |
| 7 |
Child class defines :attr:`.operation` to alter how |
| 8 |
the api definition instance is created. |
| 9 |
|
| 10 |
#. :class:`TestFullDefinedName` is to test combination of |
| 11 |
``obj.full_name`` and ``jedi.defined_names``. |
| 12 |
|
| 13 |
#. Misc single-function tests. |
| 14 |
""" |
| 15 |
|
| 16 |
import textwrap |
| 17 |
from unittest import TestCase |
| 18 |
from typing import Any |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
import jedi |
| 23 |
|
| 24 |
|
| 25 |
class MixinTestFullName(object): |
| 26 |
assertEqual: Any |
| 27 |
|
| 28 |
operation = None |
| 29 |
|
| 30 |
@pytest.fixture(autouse=True) |
| 31 |
def init(self, Script, environment): |
| 32 |
self.Script = Script |
| 33 |
self.environment = environment |
| 34 |
|
| 35 |
def check(self, source, desired): |
| 36 |
script = self.Script(textwrap.dedent(source)) |
| 37 |
definitions = getattr(script, self.operation)() |
| 38 |
for d in definitions: |
| 39 |
self.assertEqual(d.full_name, desired) |
| 40 |
|
| 41 |
def test_os_path_join(self): |
| 42 |
self.check('import os; os.path.join', 'os.path.join') |
| 43 |
|
| 44 |
def test_builtin(self): |
| 45 |
self.check('TypeError', 'builtins.TypeError') |
| 46 |
|
| 47 |
|
| 48 |
class TestFullNameWithGotoDefinitions(MixinTestFullName, TestCase): |
| 49 |
operation = 'infer' |
| 50 |
|
| 51 |
def test_tuple_mapping(self): |
| 52 |
self.check(""" |
| 53 |
import re |
| 54 |
any_re = re.compile('.*') |
| 55 |
any_re""", 're.Pattern') |
| 56 |
|
| 57 |
def test_from_import(self): |
| 58 |
self.check('from os import path', 'os.path') |
| 59 |
|
| 60 |
|
| 61 |
class TestFullNameWithCompletions(MixinTestFullName, TestCase): |
| 62 |
operation = 'complete' |
| 63 |
|
| 64 |
|
| 65 |
class TestFullDefinedName(TestCase): |
| 66 |
""" |
| 67 |
Test combination of ``obj.full_name`` and ``jedi.Script.get_names``. |
| 68 |
""" |
| 69 |
@pytest.fixture(autouse=True) |
| 70 |
def init(self, environment): |
| 71 |
self.environment = environment |
| 72 |
|
| 73 |
def check(self, source, desired): |
| 74 |
script = jedi.Script(textwrap.dedent(source), environment=self.environment) |
| 75 |
definitions = script.get_names() |
| 76 |
full_names = [d.full_name for d in definitions] |
| 77 |
self.assertEqual(full_names, desired) |
| 78 |
|
| 79 |
def test_local_names(self): |
| 80 |
self.check(""" |
| 81 |
def f(): pass |
| 82 |
class C: pass |
| 83 |
""", ['__main__.f', '__main__.C']) |
| 84 |
|
| 85 |
def test_imports(self): |
| 86 |
self.check(""" |
| 87 |
import os |
| 88 |
from os import path |
| 89 |
from os.path import join |
| 90 |
from os import path as opath |
| 91 |
""", ['os', 'os.path', 'os.path.join', 'os.path']) |
| 92 |
|
| 93 |
|
| 94 |
def test_sub_module(Script, jedi_path): |
| 95 |
""" |
| 96 |
``full_name needs to check sys.path to actually find it's real path module |
| 97 |
path. |
| 98 |
""" |
| 99 |
sys_path = [jedi_path] |
| 100 |
project = jedi.Project('.', sys_path=sys_path) |
| 101 |
defs = Script('from jedi.api import classes; classes', project=project).infer() |
| 102 |
assert [d.full_name for d in defs] == ['jedi.api.classes'] |
| 103 |
defs = Script('import jedi.api; jedi.api', project=project).infer() |
| 104 |
assert [d.full_name for d in defs] == ['jedi.api'] |
| 105 |
|
| 106 |
|
| 107 |
def test_os_path(Script): |
| 108 |
d, = Script('from os.path import join').complete() |
| 109 |
assert d.full_name == 'os.path.join' |
| 110 |
d, = Script('import os.p').complete() |
| 111 |
assert d.full_name == 'os.path' |
| 112 |
|
| 113 |
|
| 114 |
def test_os_issues(Script): |
| 115 |
"""Issue #873""" |
| 116 |
|
| 117 |
assert [c.name for c in Script('import os\nos.nt''').complete()] == [] |
| 118 |
|
| 119 |
|
| 120 |
def test_param_name(Script): |
| 121 |
name, = Script('class X:\n def foo(bar): bar''').goto() |
| 122 |
assert name.type == 'param' |
| 123 |
assert name.full_name is None |
| 124 |
|
| 125 |
|
| 126 |
def test_variable_in_func(Script): |
| 127 |
names = Script('def f(): x = 3').get_names(all_scopes=True) |
| 128 |
x = names[-1] |
| 129 |
assert x.name == 'x' |
| 130 |
assert x.full_name == '__main__.f.x' |