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)
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def names(environment):
|
||||
return partial(jedi.names, environment=environment)
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def has_typing(environment):
|
||||
if environment.version_info >= (3, 5, 0):
|
||||
|
||||
@@ -4,28 +4,21 @@ Tests for `api.defined_names`.
|
||||
|
||||
from textwrap import dedent
|
||||
|
||||
import pytest
|
||||
|
||||
from jedi import names
|
||||
from ..helpers import TestCase
|
||||
|
||||
|
||||
def _assert_definition_names(definitions, names_):
|
||||
assert [d.name for d in definitions] == names_
|
||||
|
||||
|
||||
class TestDefinedNames(TestCase):
|
||||
@pytest.fixture(autouse=True)
|
||||
def init(self, environment):
|
||||
self.environment = environment
|
||||
|
||||
def check_defined_names(self, source, names_):
|
||||
definitions = names(dedent(source), environment=self.environment)
|
||||
def _check_defined_names(names, source, names_):
|
||||
definitions = names(dedent(source))
|
||||
_assert_definition_names(definitions, names_)
|
||||
return definitions
|
||||
|
||||
def test_get_definitions_flat(self):
|
||||
self.check_defined_names("""
|
||||
|
||||
def test_get_definitions_flat(names):
|
||||
_check_defined_names(names, """
|
||||
import module
|
||||
class Class:
|
||||
pass
|
||||
@@ -34,25 +27,27 @@ class TestDefinedNames(TestCase):
|
||||
data = None
|
||||
""", ['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.y.z = None
|
||||
""", ['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):
|
||||
self.check_defined_names("""
|
||||
def test_multiple_assignment(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 another_module import *
|
||||
""", ['a', 'b'])
|
||||
|
||||
def test_nested_definitions(self):
|
||||
definitions = self.check_defined_names("""
|
||||
|
||||
def test_nested_definitions(names):
|
||||
definitions = _check_defined_names(names, """
|
||||
class Class:
|
||||
def f():
|
||||
pass
|
||||
@@ -61,11 +56,11 @@ class TestDefinedNames(TestCase):
|
||||
""", ['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'])
|
||||
assert [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_nested_class(names):
|
||||
definitions = _check_defined_names(names, """
|
||||
class L1:
|
||||
class L2:
|
||||
class L3:
|
||||
@@ -80,8 +75,9 @@ class TestDefinedNames(TestCase):
|
||||
_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("""
|
||||
|
||||
def test_class_fields_with_all_scopes_false(names):
|
||||
definitions = _check_defined_names(names, """
|
||||
from module import f
|
||||
g = f(f)
|
||||
class C:
|
||||
@@ -96,8 +92,9 @@ class TestDefinedNames(TestCase):
|
||||
_assert_definition_names(C_subdefs, ['h'])
|
||||
_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
|
||||
import asyncio
|
||||
|
||||
|
||||
Reference in New Issue
Block a user