mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 14:34:31 +08:00
use jedi.Script.method instead of stupid self calls in tests
This commit is contained in:
@@ -5,21 +5,19 @@ Unit tests to avoid errors of the past. Makes use of Python's ``unittest``
|
|||||||
module.
|
module.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import itertools
|
|
||||||
import os
|
import os
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from .base import TestBase, unittest, cwd_at
|
from .base import unittest, cwd_at
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
from jedi import Script
|
from jedi import Script
|
||||||
from jedi._compatibility import utf8, unicode
|
|
||||||
from jedi import api, parsing, common
|
from jedi import api, parsing, common
|
||||||
|
|
||||||
#jedi.set_debug_function(jedi.debug.print_to_stdout)
|
#jedi.set_debug_function(jedi.debug.print_to_stdout)
|
||||||
|
|
||||||
|
|
||||||
class TestRegression(TestBase):
|
class TestRegression(unittest.TestCase):
|
||||||
def test_goto_definition_cursor(self):
|
def test_goto_definition_cursor(self):
|
||||||
|
|
||||||
s = ("class A():\n"
|
s = ("class A():\n"
|
||||||
@@ -39,7 +37,9 @@ class TestRegression(TestBase):
|
|||||||
diff_line = 4, 10
|
diff_line = 4, 10
|
||||||
should2 = 8, 10
|
should2 = 8, 10
|
||||||
|
|
||||||
get_def = lambda pos: [d.description for d in self.goto_definitions(s, pos)]
|
def get_def(pos):
|
||||||
|
return [d.description for d in Script(s, *pos).goto_definitions()]
|
||||||
|
|
||||||
in_name = get_def(in_name)
|
in_name = get_def(in_name)
|
||||||
under_score = get_def(under_score)
|
under_score = get_def(under_score)
|
||||||
should1 = get_def(should1)
|
should1 = get_def(should1)
|
||||||
@@ -55,53 +55,53 @@ class TestRegression(TestBase):
|
|||||||
self.assertRaises(jedi.NotFoundError, get_def, cls)
|
self.assertRaises(jedi.NotFoundError, get_def, cls)
|
||||||
|
|
||||||
def test_keyword_doc(self):
|
def test_keyword_doc(self):
|
||||||
r = list(self.goto_definitions("or", (1, 1)))
|
r = list(Script("or", 1, 1).goto_definitions())
|
||||||
assert len(r) == 1
|
assert len(r) == 1
|
||||||
assert len(r[0].doc) > 100
|
assert len(r[0].doc) > 100
|
||||||
|
|
||||||
r = list(self.goto_definitions("asfdasfd", (1, 1)))
|
r = list(Script("asfdasfd", 1, 1).goto_definitions())
|
||||||
assert len(r) == 0
|
assert len(r) == 0
|
||||||
|
|
||||||
k = self.completions("fro")[0]
|
k = Script("fro").completions()[0]
|
||||||
imp_start = '\nThe ``import'
|
imp_start = '\nThe ``import'
|
||||||
assert k.raw_doc.startswith(imp_start)
|
assert k.raw_doc.startswith(imp_start)
|
||||||
assert k.doc.startswith(imp_start)
|
assert k.doc.startswith(imp_start)
|
||||||
|
|
||||||
def test_operator_doc(self):
|
def test_operator_doc(self):
|
||||||
r = list(self.goto_definitions("a == b", (1, 3)))
|
r = list(Script("a == b", 1, 3).goto_definitions())
|
||||||
assert len(r) == 1
|
assert len(r) == 1
|
||||||
assert len(r[0].doc) > 100
|
assert len(r[0].doc) > 100
|
||||||
|
|
||||||
def test_function_call_signature(self):
|
def test_function_call_signature(self):
|
||||||
defs = self.goto_definitions("""
|
defs = Script("""
|
||||||
def f(x, y=1, z='a'):
|
def f(x, y=1, z='a'):
|
||||||
pass
|
pass
|
||||||
f""")
|
f""").goto_definitions()
|
||||||
doc = defs[0].doc
|
doc = defs[0].doc
|
||||||
assert "f(x, y = 1, z = 'a')" in doc
|
assert "f(x, y = 1, z = 'a')" in doc
|
||||||
|
|
||||||
def test_class_call_signature(self):
|
def test_class_call_signature(self):
|
||||||
defs = self.goto_definitions("""
|
defs = Script("""
|
||||||
class Foo:
|
class Foo:
|
||||||
def __init__(self, x, y=1, z='a'):
|
def __init__(self, x, y=1, z='a'):
|
||||||
pass
|
pass
|
||||||
Foo""")
|
Foo""").goto_definitions()
|
||||||
doc = defs[0].doc
|
doc = defs[0].doc
|
||||||
assert "Foo(self, x, y = 1, z = 'a')" in doc
|
assert "Foo(self, x, y = 1, z = 'a')" in doc
|
||||||
|
|
||||||
def test_goto_definition_at_zero(self):
|
def test_goto_definition_at_zero(self):
|
||||||
assert self.goto_definitions("a", (1, 1)) == []
|
assert Script("a", 1, 1).goto_definitions() == []
|
||||||
s = self.goto_definitions("str", (1, 1))
|
s = Script("str", 1, 1).goto_definitions()
|
||||||
assert len(s) == 1
|
assert len(s) == 1
|
||||||
assert list(s)[0].description == 'class str'
|
assert list(s)[0].description == 'class str'
|
||||||
assert self.goto_definitions("", (1, 0)) == []
|
assert Script("", 1, 0).goto_definitions() == []
|
||||||
|
|
||||||
def test_complete_at_zero(self):
|
def test_complete_at_zero(self):
|
||||||
s = self.completions("str", (1, 3))
|
s = Script("str", 1, 3).completions()
|
||||||
assert len(s) == 1
|
assert len(s) == 1
|
||||||
assert list(s)[0].name == 'str'
|
assert list(s)[0].name == 'str'
|
||||||
|
|
||||||
s = self.completions("", (1, 0))
|
s = Script("", 1, 0).completions()
|
||||||
assert len(s) > 0
|
assert len(s) > 0
|
||||||
|
|
||||||
@cwd_at('jedi')
|
@cwd_at('jedi')
|
||||||
@@ -113,30 +113,29 @@ class TestRegression(TestBase):
|
|||||||
src2 = 'from .. import setup; setup.ret(1)'
|
src2 = 'from .. import setup; setup.ret(1)'
|
||||||
# .parser to load the module
|
# .parser to load the module
|
||||||
api.modules.Module(os.path.abspath('dynamic.py'), src2).parser
|
api.modules.Module(os.path.abspath('dynamic.py'), src2).parser
|
||||||
script = jedi.Script(src1, 1, len(src1), '../setup.py')
|
result = Script(src1, source_path='../setup.py').goto_definitions()
|
||||||
result = script.goto_definitions()
|
|
||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
assert result[0].description == 'class int'
|
assert result[0].description == 'class int'
|
||||||
|
|
||||||
def test_os_nowait(self):
|
def test_os_nowait(self):
|
||||||
""" github issue #45 """
|
""" github issue #45 """
|
||||||
s = self.completions("import os; os.P_")
|
s = Script("import os; os.P_").completions()
|
||||||
assert 'P_NOWAIT' in [i.name for i in s]
|
assert 'P_NOWAIT' in [i.name for i in s]
|
||||||
|
|
||||||
def test_keyword(self):
|
def test_keyword(self):
|
||||||
""" github jedi-vim issue #44 """
|
""" github jedi-vim issue #44 """
|
||||||
defs = self.goto_definitions("print")
|
defs = Script("print").goto_definitions()
|
||||||
assert [d.doc for d in defs]
|
assert [d.doc for d in defs]
|
||||||
|
|
||||||
defs = self.goto_definitions("import")
|
defs = Script("import").goto_definitions()
|
||||||
assert len(defs) == 1 and [1 for d in defs if d.doc]
|
assert len(defs) == 1 and [1 for d in defs if d.doc]
|
||||||
# unrelated to #44
|
# unrelated to #44
|
||||||
defs = self.goto_assignments("import")
|
defs = Script("import").goto_assignments()
|
||||||
assert len(defs) == 0
|
assert len(defs) == 0
|
||||||
completions = self.completions("import", (1,1))
|
completions = Script("import", 1,1).completions()
|
||||||
assert len(completions) == 0
|
assert len(completions) == 0
|
||||||
with common.ignored(jedi.NotFoundError): # TODO shouldn't throw that.
|
with common.ignored(jedi.NotFoundError): # TODO shouldn't throw that.
|
||||||
defs = self.goto_definitions("assert")
|
defs = Script("assert").goto_definitions()
|
||||||
assert len(defs) == 1
|
assert len(defs) == 1
|
||||||
|
|
||||||
def test_goto_assignments_keyword(self):
|
def test_goto_assignments_keyword(self):
|
||||||
@@ -145,13 +144,13 @@ class TestRegression(TestBase):
|
|||||||
|
|
||||||
'unicode' object has no attribute 'generate_call_path'
|
'unicode' object has no attribute 'generate_call_path'
|
||||||
"""
|
"""
|
||||||
self.goto_assignments('in')
|
Script('in').goto_assignments()
|
||||||
|
|
||||||
def test_points_in_completion(self):
|
def test_points_in_completion(self):
|
||||||
"""At some point, points were inserted into the completions, this
|
"""At some point, points were inserted into the completions, this
|
||||||
caused problems, sometimes.
|
caused problems, sometimes.
|
||||||
"""
|
"""
|
||||||
c = self.completions("if IndentationErr")
|
c = Script("if IndentationErr").completions()
|
||||||
assert c[0].name == 'IndentationError'
|
assert c[0].name == 'IndentationError'
|
||||||
self.assertEqual(c[0].complete, 'or')
|
self.assertEqual(c[0].complete, 'or')
|
||||||
|
|
||||||
@@ -164,8 +163,7 @@ class TestRegression(TestBase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
variable = f or C""")
|
variable = f or C""")
|
||||||
lines = source.splitlines()
|
defs = Script(source, column=3).goto_definitions()
|
||||||
defs = self.goto_definitions(source, (len(lines), 3))
|
|
||||||
defs = sorted(defs, key=lambda d: d.line)
|
defs = sorted(defs, key=lambda d: d.line)
|
||||||
self.assertEqual([d.description for d in defs],
|
self.assertEqual([d.description for d in defs],
|
||||||
['def f', 'class C'])
|
['def f', 'class C'])
|
||||||
@@ -195,7 +193,7 @@ class TestRegression(TestBase):
|
|||||||
if after_cursor in line:
|
if after_cursor in line:
|
||||||
break
|
break
|
||||||
column = len(line) - len(after_cursor)
|
column = len(line) - len(after_cursor)
|
||||||
defs = self.goto_definitions(source, (i + 1, column))
|
defs = Script(source, i + 1, column).goto_definitions()
|
||||||
self.assertEqual([d.name for d in defs], names)
|
self.assertEqual([d.name for d in defs], names)
|
||||||
|
|
||||||
def test_backslash_continuation(self):
|
def test_backslash_continuation(self):
|
||||||
@@ -210,7 +208,7 @@ class TestRegression(TestBase):
|
|||||||
|
|
||||||
# completion in whitespace
|
# completion in whitespace
|
||||||
s = 'asdfxyxxxxxxxx sds\\\n hello'
|
s = 'asdfxyxxxxxxxx sds\\\n hello'
|
||||||
assert self.goto_assignments(s, (2, 4)) == []
|
assert Script(s, 2, 4).goto_assignments() == []
|
||||||
|
|
||||||
def test_backslash_continuation_and_bracket(self):
|
def test_backslash_continuation_and_bracket(self):
|
||||||
self.check_definition_by_marker(r"""
|
self.check_definition_by_marker(r"""
|
||||||
@@ -225,4 +223,4 @@ class TestRegression(TestBase):
|
|||||||
s = "def abc():\n" \
|
s = "def abc():\n" \
|
||||||
" yield 1\n" \
|
" yield 1\n" \
|
||||||
"abc()."
|
"abc()."
|
||||||
assert self.completions(s)
|
assert Script(s).completions()
|
||||||
|
|||||||
Reference in New Issue
Block a user