mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
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):
|
|
||||||
self.environment = environment
|
|
||||||
|
|
||||||
def check_defined_names(self, source, names_):
|
|
||||||
definitions = names(dedent(source), environment=self.environment)
|
|
||||||
_assert_definition_names(definitions, names_)
|
_assert_definition_names(definitions, names_)
|
||||||
return definitions
|
return definitions
|
||||||
|
|
||||||
def test_get_definitions_flat(self):
|
|
||||||
self.check_defined_names("""
|
def test_get_definitions_flat(names):
|
||||||
|
_check_defined_names(names, """
|
||||||
import module
|
import module
|
||||||
class Class:
|
class Class:
|
||||||
pass
|
pass
|
||||||
@@ -34,25 +27,27 @@ class TestDefinedNames(TestCase):
|
|||||||
data = None
|
data = None
|
||||||
""", ['module', 'Class', 'func', 'data'])
|
""", ['module', 'Class', 'func', 'data'])
|
||||||
|
|
||||||
def test_dotted_assignment(self):
|
|
||||||
self.check_defined_names("""
|
def test_dotted_assignment(names):
|
||||||
|
_check_defined_names(names, """
|
||||||
x = Class()
|
x = Class()
|
||||||
x.y.z = None
|
x.y.z = None
|
||||||
""", ['x', 'z']) # TODO is this behavior what we want?
|
""", ['x', 'z']) # TODO is this behavior what we want?
|
||||||
|
|
||||||
def test_multiple_assignment(self):
|
|
||||||
self.check_defined_names("""
|
|
||||||
x = y = None
|
|
||||||
""", ['x', 'y'])
|
|
||||||
|
|
||||||
def test_multiple_imports(self):
|
def test_multiple_assignment(names):
|
||||||
self.check_defined_names("""
|
_check_defined_names(names, "x = y = None", ['x', 'y'])
|
||||||
|
|
||||||
|
|
||||||
|
def test_multiple_imports(names):
|
||||||
|
_check_defined_names(names, """
|
||||||
from module import a, b
|
from module import a, b
|
||||||
from another_module import *
|
from another_module import *
|
||||||
""", ['a', 'b'])
|
""", ['a', 'b'])
|
||||||
|
|
||||||
def test_nested_definitions(self):
|
|
||||||
definitions = self.check_defined_names("""
|
def test_nested_definitions(names):
|
||||||
|
definitions = _check_defined_names(names, """
|
||||||
class Class:
|
class Class:
|
||||||
def f():
|
def f():
|
||||||
pass
|
pass
|
||||||
@@ -61,11 +56,11 @@ class TestDefinedNames(TestCase):
|
|||||||
""", ['Class'])
|
""", ['Class'])
|
||||||
subdefinitions = definitions[0].defined_names()
|
subdefinitions = definitions[0].defined_names()
|
||||||
_assert_definition_names(subdefinitions, ['f', 'g'])
|
_assert_definition_names(subdefinitions, ['f', 'g'])
|
||||||
self.assertEqual([d.full_name for d in subdefinitions],
|
assert [d.full_name for d in subdefinitions] == ['__main__.Class.f', '__main__.Class.g']
|
||||||
['__main__.Class.f', '__main__.Class.g'])
|
|
||||||
|
|
||||||
def test_nested_class(self):
|
|
||||||
definitions = self.check_defined_names("""
|
def test_nested_class(names):
|
||||||
|
definitions = _check_defined_names(names, """
|
||||||
class L1:
|
class L1:
|
||||||
class L2:
|
class L2:
|
||||||
class L3:
|
class L3:
|
||||||
@@ -80,8 +75,9 @@ class TestDefinedNames(TestCase):
|
|||||||
_assert_definition_names(subsubdefs, ['L3', 'f'])
|
_assert_definition_names(subsubdefs, ['L3', 'f'])
|
||||||
_assert_definition_names(subsubdefs[0].defined_names(), ['f'])
|
_assert_definition_names(subsubdefs[0].defined_names(), ['f'])
|
||||||
|
|
||||||
def test_class_fields_with_all_scopes_false(self):
|
|
||||||
definitions = self.check_defined_names("""
|
def test_class_fields_with_all_scopes_false(names):
|
||||||
|
definitions = _check_defined_names(names, """
|
||||||
from module import f
|
from module import f
|
||||||
g = f(f)
|
g = f(f)
|
||||||
class C:
|
class C:
|
||||||
@@ -96,8 +92,9 @@ class TestDefinedNames(TestCase):
|
|||||||
_assert_definition_names(C_subdefs, ['h'])
|
_assert_definition_names(C_subdefs, ['h'])
|
||||||
_assert_definition_names(foo_subdefs, ['x', 'bar'])
|
_assert_definition_names(foo_subdefs, ['x', 'bar'])
|
||||||
|
|
||||||
def test_async_stmt_with_all_scopes_false(self):
|
|
||||||
definitions = self.check_defined_names("""
|
def test_async_stmt_with_all_scopes_false(names):
|
||||||
|
definitions = _check_defined_names(names, """
|
||||||
from module import f
|
from module import f
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user