mirror of
https://github.com/davidhalter/jedi.git
synced 2026-10-26 17:23:17 +08:00
173 lines
5.2 KiB
Python
173 lines
5.2 KiB
Python
"""
|
|
Tests for :attr:`.BaseName.full_name`.
|
|
|
|
There are three kinds of test:
|
|
|
|
#. Test classes derived from :class:`MixinTestFullName`.
|
|
Child class defines :attr:`.operation` to alter how
|
|
the api definition instance is created.
|
|
|
|
#. :class:`TestFullDefinedName` is to test combination of
|
|
``obj.full_name`` and ``jedi.defined_names``.
|
|
|
|
#. Misc single-function tests.
|
|
"""
|
|
|
|
import textwrap
|
|
from unittest import TestCase
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
import jedi
|
|
|
|
|
|
class MixinTestFullName(object):
|
|
assertEqual: Any
|
|
|
|
operation = None
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def init(self, Script, environment):
|
|
self.Script = Script
|
|
self.environment = environment
|
|
|
|
def check(self, source, desired):
|
|
script = self.Script(textwrap.dedent(source))
|
|
definitions = getattr(script, self.operation)()
|
|
for d in definitions:
|
|
self.assertEqual(d.full_name, desired)
|
|
|
|
def test_os_path_join(self):
|
|
self.check('import os; os.path.join', 'os.path.join')
|
|
|
|
def test_builtin(self):
|
|
self.check('TypeError', 'builtins.TypeError')
|
|
|
|
|
|
class TestFullNameWithGotoDefinitions(MixinTestFullName, TestCase):
|
|
operation = 'infer'
|
|
|
|
def test_tuple_mapping(self):
|
|
self.check("""
|
|
import re
|
|
any_re = re.compile('.*')
|
|
any_re""", 're.Pattern')
|
|
|
|
def test_from_import(self):
|
|
self.check('from os import path', 'os.path')
|
|
|
|
|
|
class TestFullNameWithCompletions(MixinTestFullName, TestCase):
|
|
operation = 'complete'
|
|
|
|
|
|
class TestFullDefinedName(TestCase):
|
|
"""
|
|
Test combination of ``obj.full_name`` and ``jedi.Script.get_names``.
|
|
"""
|
|
@pytest.fixture(autouse=True)
|
|
def init(self, environment):
|
|
self.environment = environment
|
|
|
|
def check(self, source, desired):
|
|
script = jedi.Script(textwrap.dedent(source), environment=self.environment)
|
|
definitions = script.get_names()
|
|
full_names = [d.full_name for d in definitions]
|
|
self.assertEqual(full_names, desired)
|
|
|
|
def test_local_names(self):
|
|
self.check("""
|
|
def f(): pass
|
|
class C: pass
|
|
""", ['__main__.f', '__main__.C'])
|
|
|
|
def test_imports(self):
|
|
self.check("""
|
|
import os
|
|
from os import path
|
|
from os.path import join
|
|
from os import path as opath
|
|
""", ['os', 'os.path', 'os.path.join', 'os.path'])
|
|
|
|
|
|
def test_sub_module(Script, jedi_path):
|
|
"""
|
|
``full_name needs to check sys.path to actually find it's real path module
|
|
path.
|
|
"""
|
|
sys_path = [jedi_path]
|
|
project = jedi.Project('.', sys_path=sys_path)
|
|
defs = Script('from jedi.api import classes; classes', project=project).infer()
|
|
assert [d.full_name for d in defs] == ['jedi.api.classes']
|
|
defs = Script('import jedi.api; jedi.api', project=project).infer()
|
|
assert [d.full_name for d in defs] == ['jedi.api']
|
|
|
|
|
|
def test_os_path(Script):
|
|
d, = Script('from os.path import join').complete()
|
|
assert d.full_name == 'os.path.join'
|
|
d, = Script('import os.p').complete()
|
|
assert d.full_name == 'os.path'
|
|
|
|
|
|
def test_os_issues(Script):
|
|
"""Issue #873"""
|
|
# nt is not found, because it's deleted
|
|
assert [c.name for c in Script('import os\nos.nt''').complete()] == []
|
|
|
|
|
|
def test_param_name(Script):
|
|
name, = Script('class X:\n def foo(bar): bar''').goto()
|
|
assert name.type == 'param'
|
|
assert name.full_name is None
|
|
|
|
|
|
def test_variable_in_func(Script):
|
|
names = Script('def f(): x = 3').get_names(all_scopes=True)
|
|
x = names[-1]
|
|
assert x.name == 'x'
|
|
assert x.full_name == '__main__.f.x'
|
|
|
|
|
|
def test_comprehension_full_name_issue_1944(Script):
|
|
source = textwrap.dedent('''\
|
|
def pig_latin(text):
|
|
translated_words = [translate(word) for word in words]
|
|
''')
|
|
names = Script(source).get_names(all_scopes=True, references=True)
|
|
word_names = [name for name in names if name.name == 'word']
|
|
assert len(word_names) == 2
|
|
assert {name.full_name for name in word_names} == {
|
|
'__main__.pig_latin.<comprehension>.word'
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize('expression', [
|
|
'[word for word in words]',
|
|
'{word for word in words}',
|
|
'{word: word for word in words}',
|
|
'(word for word in words)',
|
|
'[word for row in rows for word in row]',
|
|
'[word for row in rows if row for word in row]',
|
|
])
|
|
def test_comprehension_full_name(Script, expression):
|
|
source = 'def f():\n return ' + expression
|
|
names = Script(source).get_names(all_scopes=True, references=True)
|
|
word_names = [name for name in names if name.name == 'word']
|
|
assert len(word_names) >= 2
|
|
assert {name.full_name for name in word_names} == {'__main__.f.<comprehension>.word'}
|
|
|
|
|
|
@pytest.mark.parametrize('source, expected', [
|
|
('[word for word in words]', '__main__.<comprehension>.word'),
|
|
('def f():\n return [[word for word in words] for words in rows]',
|
|
'__main__.f.<comprehension>.<comprehension>.word'),
|
|
('def outer():\n def f():\n return [word for word in words]', None),
|
|
])
|
|
def test_comprehension_parent_full_name(Script, source, expected):
|
|
names = Script(source).get_names(all_scopes=True, references=True)
|
|
word_names = [name for name in names if name.name == 'word']
|
|
assert len(word_names) == 2
|
|
assert {name.full_name for name in word_names} == {expected}
|