forked from VimPlug/jedi
Refactor names tests
This commit is contained in:
@@ -105,6 +105,11 @@ def Script(environment):
|
|||||||
return partial(jedi.Script, environment=environment)
|
return partial(jedi.Script, environment=environment)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session')
|
||||||
|
def names(environment):
|
||||||
|
return partial(jedi.names, environment=environment)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def has_typing(environment):
|
def has_typing(environment):
|
||||||
if environment.version_info >= (3, 5, 0):
|
if environment.version_info >= (3, 5, 0):
|
||||||
|
|||||||
@@ -4,28 +4,21 @@ Tests for `api.defined_names`.
|
|||||||
|
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from jedi import names
|
from jedi import names
|
||||||
from ..helpers import TestCase
|
|
||||||
|
|
||||||
|
|
||||||
def _assert_definition_names(definitions, names_):
|
def _assert_definition_names(definitions, names_):
|
||||||
assert [d.name for d in definitions] == names_
|
assert [d.name for d in definitions] == names_
|
||||||
|
|
||||||
|
|
||||||
class TestDefinedNames(TestCase):
|
def _check_defined_names(names, source, names_):
|
||||||
@pytest.fixture(autouse=True)
|
definitions = names(dedent(source))
|
||||||
def init(self, environment):
|
_assert_definition_names(definitions, names_)
|
||||||
self.environment = environment
|
return definitions
|
||||||
|
|
||||||
def check_defined_names(self, source, names_):
|
|
||||||
definitions = names(dedent(source), environment=self.environment)
|
|
||||||
_assert_definition_names(definitions, names_)
|
|
||||||
return definitions
|
|
||||||
|
|
||||||
def test_get_definitions_flat(self):
|
def test_get_definitions_flat(names):
|
||||||
self.check_defined_names("""
|
_check_defined_names(names, """
|
||||||
import module
|
import module
|
||||||
class Class:
|
class Class:
|
||||||
pass
|
pass
|
||||||
@@ -34,105 +27,109 @@ class TestDefinedNames(TestCase):
|
|||||||
data = None
|
data = None
|
||||||
""", ['module', 'Class', 'func', 'data'])
|
""", ['module', 'Class', 'func', 'data'])
|
||||||
|
|
||||||
def test_dotted_assignment(self):
|
|
||||||
self.check_defined_names("""
|
|
||||||
x = Class()
|
|
||||||
x.y.z = None
|
|
||||||
""", ['x', 'z']) # TODO is this behavior what we want?
|
|
||||||
|
|
||||||
def test_multiple_assignment(self):
|
def test_dotted_assignment(names):
|
||||||
self.check_defined_names("""
|
_check_defined_names(names, """
|
||||||
x = y = None
|
x = Class()
|
||||||
""", ['x', 'y'])
|
x.y.z = None
|
||||||
|
""", ['x', 'z']) # TODO is this behavior what we want?
|
||||||
|
|
||||||
def test_multiple_imports(self):
|
|
||||||
self.check_defined_names("""
|
|
||||||
from module import a, b
|
|
||||||
from another_module import *
|
|
||||||
""", ['a', 'b'])
|
|
||||||
|
|
||||||
def test_nested_definitions(self):
|
def test_multiple_assignment(names):
|
||||||
definitions = self.check_defined_names("""
|
_check_defined_names(names, "x = y = None", ['x', 'y'])
|
||||||
class Class:
|
|
||||||
def f():
|
|
||||||
pass
|
|
||||||
def g():
|
|
||||||
pass
|
|
||||||
""", ['Class'])
|
|
||||||
subdefinitions = definitions[0].defined_names()
|
|
||||||
_assert_definition_names(subdefinitions, ['f', 'g'])
|
|
||||||
self.assertEqual([d.full_name for d in subdefinitions],
|
|
||||||
['__main__.Class.f', '__main__.Class.g'])
|
|
||||||
|
|
||||||
def test_nested_class(self):
|
|
||||||
definitions = self.check_defined_names("""
|
def test_multiple_imports(names):
|
||||||
class L1:
|
_check_defined_names(names, """
|
||||||
class L2:
|
from module import a, b
|
||||||
class L3:
|
from another_module import *
|
||||||
def f(): pass
|
""", ['a', 'b'])
|
||||||
|
|
||||||
|
|
||||||
|
def test_nested_definitions(names):
|
||||||
|
definitions = _check_defined_names(names, """
|
||||||
|
class Class:
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
def g():
|
||||||
|
pass
|
||||||
|
""", ['Class'])
|
||||||
|
subdefinitions = definitions[0].defined_names()
|
||||||
|
_assert_definition_names(subdefinitions, ['f', 'g'])
|
||||||
|
assert [d.full_name for d in subdefinitions] == ['__main__.Class.f', '__main__.Class.g']
|
||||||
|
|
||||||
|
|
||||||
|
def test_nested_class(names):
|
||||||
|
definitions = _check_defined_names(names, """
|
||||||
|
class L1:
|
||||||
|
class L2:
|
||||||
|
class L3:
|
||||||
def f(): pass
|
def f(): pass
|
||||||
def f(): pass
|
def f(): pass
|
||||||
def f(): pass
|
def f(): pass
|
||||||
""", ['L1', 'f'])
|
def f(): pass
|
||||||
subdefs = definitions[0].defined_names()
|
""", ['L1', 'f'])
|
||||||
subsubdefs = subdefs[0].defined_names()
|
subdefs = definitions[0].defined_names()
|
||||||
_assert_definition_names(subdefs, ['L2', 'f'])
|
subsubdefs = subdefs[0].defined_names()
|
||||||
_assert_definition_names(subsubdefs, ['L3', 'f'])
|
_assert_definition_names(subdefs, ['L2', 'f'])
|
||||||
_assert_definition_names(subsubdefs[0].defined_names(), ['f'])
|
_assert_definition_names(subsubdefs, ['L3', 'f'])
|
||||||
|
_assert_definition_names(subsubdefs[0].defined_names(), ['f'])
|
||||||
|
|
||||||
def test_class_fields_with_all_scopes_false(self):
|
|
||||||
definitions = self.check_defined_names("""
|
|
||||||
from module import f
|
|
||||||
g = f(f)
|
|
||||||
class C:
|
|
||||||
h = g
|
|
||||||
|
|
||||||
def foo(x=a):
|
def test_class_fields_with_all_scopes_false(names):
|
||||||
bar = x
|
definitions = _check_defined_names(names, """
|
||||||
return bar
|
from module import f
|
||||||
""", ['f', 'g', 'C', 'foo'])
|
g = f(f)
|
||||||
C_subdefs = definitions[-2].defined_names()
|
class C:
|
||||||
foo_subdefs = definitions[-1].defined_names()
|
h = g
|
||||||
_assert_definition_names(C_subdefs, ['h'])
|
|
||||||
_assert_definition_names(foo_subdefs, ['x', 'bar'])
|
|
||||||
|
|
||||||
def test_async_stmt_with_all_scopes_false(self):
|
def foo(x=a):
|
||||||
definitions = self.check_defined_names("""
|
bar = x
|
||||||
from module import f
|
return bar
|
||||||
import asyncio
|
""", ['f', 'g', 'C', 'foo'])
|
||||||
|
C_subdefs = definitions[-2].defined_names()
|
||||||
|
foo_subdefs = definitions[-1].defined_names()
|
||||||
|
_assert_definition_names(C_subdefs, ['h'])
|
||||||
|
_assert_definition_names(foo_subdefs, ['x', 'bar'])
|
||||||
|
|
||||||
g = f(f)
|
|
||||||
class C:
|
|
||||||
h = g
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def __aenter__(self):
|
def test_async_stmt_with_all_scopes_false(names):
|
||||||
pass
|
definitions = _check_defined_names(names, """
|
||||||
|
from module import f
|
||||||
|
import asyncio
|
||||||
|
|
||||||
def foo(x=a):
|
g = f(f)
|
||||||
bar = x
|
class C:
|
||||||
return bar
|
h = g
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
async def async_foo(duration):
|
async def __aenter__(self):
|
||||||
async def wait():
|
pass
|
||||||
await asyncio.sleep(100)
|
|
||||||
for i in range(duration//100):
|
|
||||||
await wait()
|
|
||||||
return duration//100*100
|
|
||||||
|
|
||||||
async with C() as cinst:
|
def foo(x=a):
|
||||||
d = cinst
|
bar = x
|
||||||
""", ['f', 'asyncio', 'g', 'C', 'foo', 'async_foo', 'cinst', 'd'])
|
return bar
|
||||||
C_subdefs = definitions[3].defined_names()
|
|
||||||
foo_subdefs = definitions[4].defined_names()
|
async def async_foo(duration):
|
||||||
async_foo_subdefs = definitions[5].defined_names()
|
async def wait():
|
||||||
cinst_subdefs = definitions[6].defined_names()
|
await asyncio.sleep(100)
|
||||||
_assert_definition_names(C_subdefs, ['h', '__init__', '__aenter__'])
|
for i in range(duration//100):
|
||||||
_assert_definition_names(foo_subdefs, ['x', 'bar'])
|
await wait()
|
||||||
_assert_definition_names(async_foo_subdefs, ['duration', 'wait', 'i'])
|
return duration//100*100
|
||||||
# We treat d as a name outside `async with` block
|
|
||||||
_assert_definition_names(cinst_subdefs, [])
|
async with C() as cinst:
|
||||||
|
d = cinst
|
||||||
|
""", ['f', 'asyncio', 'g', 'C', 'foo', 'async_foo', 'cinst', 'd'])
|
||||||
|
C_subdefs = definitions[3].defined_names()
|
||||||
|
foo_subdefs = definitions[4].defined_names()
|
||||||
|
async_foo_subdefs = definitions[5].defined_names()
|
||||||
|
cinst_subdefs = definitions[6].defined_names()
|
||||||
|
_assert_definition_names(C_subdefs, ['h', '__init__', '__aenter__'])
|
||||||
|
_assert_definition_names(foo_subdefs, ['x', 'bar'])
|
||||||
|
_assert_definition_names(async_foo_subdefs, ['duration', 'wait', 'i'])
|
||||||
|
# We treat d as a name outside `async with` block
|
||||||
|
_assert_definition_names(cinst_subdefs, [])
|
||||||
|
|
||||||
|
|
||||||
def test_follow_imports(environment):
|
def test_follow_imports(environment):
|
||||||
|
|||||||
Reference in New Issue
Block a user