refactor follow_definition tests

This commit is contained in:
Dave Halter
2014-03-23 12:40:20 +01:00
parent 67202db305
commit 5aa6c770b3
3 changed files with 63 additions and 77 deletions

View File

@@ -0,0 +1,62 @@
from itertools import chain
import jedi
from ..helpers import cwd_at
def test_import_empty():
""" github #340, return the full word. """
completion = jedi.Script("import ").completions()[0]
definition = completion.follow_definition()[0]
assert definition
def check_follow_definition_types(source):
# nested import
completions = jedi.Script(source, path='some_path.py').completions()
defs = chain.from_iterable(c.follow_definition() for c in completions)
return [d.type for d in defs]
def test_follow_import_incomplete():
"""
Completion on incomplete imports should always take the full completion
to do any evaluation.
"""
datetime = check_follow_definition_types("import itertool")
assert datetime == ['module']
# empty `from * import` parts
itert = jedi.Script("from itertools import ").completions()
definitions = [d for d in itert if d.name == 'chain']
assert len(definitions) == 1
assert [d.type for d in definitions[0].follow_definition()] == ['class']
# incomplete `from * import` part
datetime = check_follow_definition_types("from datetime import datetim")
assert set(datetime) == set(['class']) # py33: builtin and pure py version
# os.path check
ospath = check_follow_definition_types("from os.path import abspat")
assert ospath == ['function']
# alias
alias = check_follow_definition_types("import io as abcd; abcd")
assert alias == ['module']
@cwd_at('test/completion/import_tree')
def test_follow_definition_nested_import():
types = check_follow_definition_types("import pkg.mod1; pkg")
assert types == ['module']
types = check_follow_definition_types("import pkg.mod1; pkg.mod1")
assert types == ['module']
types = check_follow_definition_types("import pkg.mod1; pkg.mod1.a")
assert types == ['class']
def test_follow_definition_land_on_import():
types = check_follow_definition_types("import datetime; datetim")
assert types == ['module']

View File

@@ -1,5 +1,3 @@
from itertools import chain
import pytest
import jedi
@@ -25,64 +23,3 @@ def test_import_not_in_sys_path():
assert a[0].name == 'str'
a = jedi.Script(path='module.py', line=7).goto_definitions()
assert a[0].name == 'str'
def test_import_empty():
""" github #340, return the full word. """
completion = jedi.Script("import ").completions()[0]
definition = completion.follow_definition()[0]
print(definition)
assert definition
def check_follow_definition_types(source):
# nested import
completions = jedi.Script(source, path='some_path.py').completions()
defs = chain.from_iterable(c.follow_definition() for c in completions)
return [d.type for d in defs]
def test_follow_import_incomplete():
"""
Completion on incomplete imports should always take the full completion
to do any evaluation.
"""
datetime = check_follow_definition_types("import itertool")
assert datetime == ['module']
# empty `from * import` parts
itert = jedi.Script("from itertools import ").completions()
definitions = [d for d in itert if d.name == 'chain']
assert len(definitions) == 1
assert [d.type for d in definitions[0].follow_definition()] == ['class']
# incomplete `from * import` part
datetime = check_follow_definition_types("from datetime import datetim")
assert set(datetime) == set(['class']) # py33: builtin and pure py version
# os.path check
ospath = check_follow_definition_types("from os.path import abspat")
assert ospath == ['function']
# alias
alias = check_follow_definition_types("import io as abcd; abcd")
assert alias == ['module']
@cwd_at('test/completion/import_tree')
def test_follow_definition_nested_import():
types = check_follow_definition_types("import pkg.mod1; pkg")
assert types == ['module']
types = check_follow_definition_types("import pkg.mod1; pkg.mod1")
assert types == ['module']
types = check_follow_definition_types("import pkg.mod1; pkg.mod1.a")
assert types == ['class']
def test_follow_definition_land_on_import():
datetime = jedi.Script("import datetime; datetim").completions()[0]
definition = datetime.follow_definition()[0]
print(datetime._definition, definition)
assert definition.type == 'module'

View File

@@ -2,9 +2,6 @@
Tests of various import related things that could not be tested with "Black Box
Tests".
"""
import itertools
from jedi import Script
from .helpers import cwd_at
from jedi._compatibility import is_py26
@@ -34,7 +31,7 @@ def test_imports_on_global_namespace_without_path():
"""If the path is None, there shouldn't be any import problem"""
completions = Script("import operator").completions()
assert [c.name for c in completions] == ['operator']
completions = Script("import operator", path= 'example.py').completions()
completions = Script("import operator", path='example.py').completions()
assert [c.name for c in completions] == ['operator']
# the first one has a path the second doesn't
@@ -69,13 +66,3 @@ def test_after_from():
check('from os\\\n', ['import'])
check('from os \\\n', ['import'])
def test_follow_definition():
""" github issue #45 """
c = Script("from datetime import timedelta; timedelta").completions()
# type can also point to import, but there will be additional
# attributes
objs = itertools.chain.from_iterable(r.follow_definition() for r in c)
types = [o.type for o in objs]
assert 'import' not in types and 'class' in types