forked from VimPlug/jedi
Merge pull request #149 from tkf/definition-in-call
Fallback to callee definition when definition not found (fixes #131)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import unittest
|
||||
|
||||
import time
|
||||
import sys
|
||||
if sys.hexversion < 0x02070000:
|
||||
import unittest2 as unittest
|
||||
else:
|
||||
import unittest
|
||||
import os
|
||||
from os.path import abspath, dirname
|
||||
import functools
|
||||
|
||||
65
test/completion/definition.py
Normal file
65
test/completion/definition.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""
|
||||
Fallback to callee definition when definition not found.
|
||||
- https://github.com/davidhalter/jedi/issues/131
|
||||
- https://github.com/davidhalter/jedi/pull/149
|
||||
"""
|
||||
|
||||
"""Parenthesis closed at next line."""
|
||||
|
||||
#? isinstance
|
||||
isinstance(
|
||||
)
|
||||
|
||||
#? isinstance
|
||||
isinstance(
|
||||
)
|
||||
|
||||
#? isinstance
|
||||
isinstance(None,
|
||||
)
|
||||
|
||||
#? isinstance
|
||||
isinstance(None,
|
||||
)
|
||||
|
||||
"""Parenthesis closed at same line."""
|
||||
|
||||
# Note: len('isinstance(') == 11
|
||||
#? 11 isinstance
|
||||
isinstance()
|
||||
|
||||
# Note: len('isinstance(None,') == 16
|
||||
##? 16 isinstance
|
||||
isinstance(None,)
|
||||
|
||||
# Note: len('isinstance(None,') == 16
|
||||
##? 16 isinstance
|
||||
isinstance(None, )
|
||||
|
||||
# Note: len('isinstance(None, ') == 17
|
||||
##? 17 isinstance
|
||||
isinstance(None, )
|
||||
|
||||
# Note: len('isinstance( ') == 12
|
||||
##? 12 isinstance
|
||||
isinstance( )
|
||||
|
||||
"""Unclosed parenthesis."""
|
||||
|
||||
#? isinstance
|
||||
isinstance(
|
||||
|
||||
def x(): pass # acts like EOF
|
||||
|
||||
##? isinstance
|
||||
isinstance(
|
||||
|
||||
def x(): pass # acts like EOF
|
||||
|
||||
#? isinstance
|
||||
isinstance(None,
|
||||
|
||||
def x(): pass # acts like EOF
|
||||
|
||||
#? isinstance
|
||||
isinstance(None,
|
||||
@@ -5,17 +5,18 @@ Unit tests to avoid errors of the past. Makes use of Python's ``unittest``
|
||||
module.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import time
|
||||
import functools
|
||||
import itertools
|
||||
import os
|
||||
import textwrap
|
||||
|
||||
from base import TestBase, cwd_at
|
||||
from base import TestBase, unittest, cwd_at
|
||||
|
||||
import jedi
|
||||
from jedi._compatibility import is_py25, utf8, unicode
|
||||
from jedi import api
|
||||
from jedi import api_classes
|
||||
|
||||
#jedi.set_debug_function(jedi.debug.print_to_stdout)
|
||||
|
||||
@@ -145,10 +146,15 @@ class TestRegression(TestBase):
|
||||
assert self.complete("from datetime import")[0].word == 'import'
|
||||
assert self.complete("from datetime import ")
|
||||
|
||||
def assert_call_def(self, call_def, name, index):
|
||||
self.assertEqual(
|
||||
{'call_name': getattr(call_def, 'call_name', None),
|
||||
'index': getattr(call_def, 'index', None)},
|
||||
{'call_name': name, 'index': index},
|
||||
)
|
||||
|
||||
def test_function_definition(self):
|
||||
def check(call_def, name, index):
|
||||
return call_def and call_def.call_name == name \
|
||||
and call_def.index == index
|
||||
check = self.assert_call_def
|
||||
|
||||
# simple
|
||||
s = "abs(a, str("
|
||||
@@ -161,13 +167,13 @@ class TestRegression(TestBase):
|
||||
s7 = "str().upper().center("
|
||||
s8 = "str(int[zip("
|
||||
|
||||
assert check(self.function_definition(s, (1, 4)), 'abs', 0)
|
||||
assert check(self.function_definition(s, (1, 6)), 'abs', 1)
|
||||
assert check(self.function_definition(s, (1, 7)), 'abs', 1)
|
||||
assert check(self.function_definition(s, (1, 8)), 'abs', 1)
|
||||
assert check(self.function_definition(s, (1, 11)), 'str', 0)
|
||||
check(self.function_definition(s, (1, 4)), 'abs', 0)
|
||||
check(self.function_definition(s, (1, 6)), 'abs', 1)
|
||||
check(self.function_definition(s, (1, 7)), 'abs', 1)
|
||||
check(self.function_definition(s, (1, 8)), 'abs', 1)
|
||||
check(self.function_definition(s, (1, 11)), 'str', 0)
|
||||
|
||||
assert check(self.function_definition(s2, (1, 4)), 'abs', 0)
|
||||
check(self.function_definition(s2, (1, 4)), 'abs', 0)
|
||||
assert self.function_definition(s2, (1, 5)) is None
|
||||
assert self.function_definition(s2) is None
|
||||
|
||||
@@ -175,43 +181,41 @@ class TestRegression(TestBase):
|
||||
assert self.function_definition(s3) is None
|
||||
|
||||
assert self.function_definition(s4, (1, 3)) is None
|
||||
assert check(self.function_definition(s4, (1, 4)), 'abs', 0)
|
||||
assert check(self.function_definition(s4, (1, 8)), 'zip', 0)
|
||||
assert check(self.function_definition(s4, (1, 9)), 'abs', 0)
|
||||
#assert check(self.function_definition(s4, (1, 10)), 'abs', 1)
|
||||
check(self.function_definition(s4, (1, 4)), 'abs', 0)
|
||||
check(self.function_definition(s4, (1, 8)), 'zip', 0)
|
||||
check(self.function_definition(s4, (1, 9)), 'abs', 0)
|
||||
#check(self.function_definition(s4, (1, 10)), 'abs', 1)
|
||||
|
||||
assert check(self.function_definition(s5, (1, 4)), 'abs', 0)
|
||||
assert check(self.function_definition(s5, (1, 6)), 'abs', 1)
|
||||
check(self.function_definition(s5, (1, 4)), 'abs', 0)
|
||||
check(self.function_definition(s5, (1, 6)), 'abs', 1)
|
||||
|
||||
assert check(self.function_definition(s6), 'center', 0)
|
||||
assert check(self.function_definition(s6, (1, 4)), 'str', 0)
|
||||
check(self.function_definition(s6), 'center', 0)
|
||||
check(self.function_definition(s6, (1, 4)), 'str', 0)
|
||||
|
||||
assert check(self.function_definition(s7), 'center', 0)
|
||||
assert check(self.function_definition(s8), 'zip', 0)
|
||||
assert check(self.function_definition(s8, (1, 8)), 'str', 0)
|
||||
check(self.function_definition(s7), 'center', 0)
|
||||
check(self.function_definition(s8), 'zip', 0)
|
||||
check(self.function_definition(s8, (1, 8)), 'str', 0)
|
||||
|
||||
s = "import time; abc = time; abc.sleep("
|
||||
assert check(self.function_definition(s), 'sleep', 0)
|
||||
check(self.function_definition(s), 'sleep', 0)
|
||||
|
||||
# jedi-vim #9
|
||||
s = "with open("
|
||||
assert check(self.function_definition(s), 'open', 0)
|
||||
check(self.function_definition(s), 'open', 0)
|
||||
|
||||
# jedi-vim #11
|
||||
s1 = "for sorted("
|
||||
assert check(self.function_definition(s1), 'sorted', 0)
|
||||
check(self.function_definition(s1), 'sorted', 0)
|
||||
s2 = "for s in sorted("
|
||||
assert check(self.function_definition(s2), 'sorted', 0)
|
||||
check(self.function_definition(s2), 'sorted', 0)
|
||||
|
||||
# jedi #57
|
||||
s = "def func(alpha, beta): pass\n" \
|
||||
"func(alpha='101',"
|
||||
assert check(self.function_definition(s, (2, 13)), 'func', 0)
|
||||
check(self.function_definition(s, (2, 13)), 'func', 0)
|
||||
|
||||
def test_function_definition_complex(self):
|
||||
def check(call_def, name, index):
|
||||
return call_def and call_def.call_name == name \
|
||||
and call_def.index == index
|
||||
check = self.assert_call_def
|
||||
|
||||
s = """
|
||||
def abc(a,b):
|
||||
@@ -223,18 +227,27 @@ class TestRegression(TestBase):
|
||||
if 1:
|
||||
pass
|
||||
"""
|
||||
assert check(self.function_definition(s, (6, 24)), 'abc', 0)
|
||||
check(self.function_definition(s, (6, 24)), 'abc', 0)
|
||||
s = """
|
||||
import re
|
||||
def huhu(it):
|
||||
re.compile(
|
||||
return it * 2
|
||||
"""
|
||||
assert check(self.function_definition(s, (4, 31)), 'compile', 0)
|
||||
check(self.function_definition(s, (4, 31)), 'compile', 0)
|
||||
# jedi-vim #70
|
||||
s = """def foo("""
|
||||
assert self.function_definition(s) is None
|
||||
|
||||
@unittest.skip("function_definition at ``f( |)`` does not work")
|
||||
def test_function_definition_empty_paren_pre_space(self):
|
||||
s = textwrap.dedent("""\
|
||||
def f(a, b):
|
||||
pass
|
||||
f( )""")
|
||||
call_def = self.function_definition(s, (3, 3))
|
||||
self.assert_call_def(call_def, 'f', 0)
|
||||
|
||||
@cwd_at('jedi')
|
||||
def test_add_dynamic_mods(self):
|
||||
api.settings.additional_dynamic_modules = ['dynamic.py']
|
||||
@@ -384,9 +397,14 @@ class TestFeature(TestBase):
|
||||
""" feature request #61"""
|
||||
assert self.complete('import os; os.path.join')[0].full_name \
|
||||
== 'os.path.join'
|
||||
# issue #94
|
||||
defs = self.definition("""import os; os.path.join(""")
|
||||
assert defs[0].full_name is None
|
||||
|
||||
def test_keyword_full_name_should_be_none(self):
|
||||
"""issue #94"""
|
||||
# Using `from jedi.keywords import Keyword` here does NOT work
|
||||
# in Python 3. This is due to the import hack jedi using.
|
||||
Keyword = api_classes.keywords.Keyword
|
||||
d = api_classes.Definition(Keyword('(', (0, 0)))
|
||||
assert d.full_name is None
|
||||
|
||||
def test_full_name_builtin(self):
|
||||
self.assertEqual(self.complete('type')[0].full_name, 'type')
|
||||
|
||||
Reference in New Issue
Block a user