mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Use the Script fixture more generally
This commit is contained in:
@@ -6,19 +6,20 @@ from inspect import cleandoc
|
||||
|
||||
import pytest
|
||||
|
||||
from jedi import Script, __doc__ as jedi_doc, names
|
||||
import jedi
|
||||
from jedi import __doc__ as jedi_doc, names
|
||||
from ..helpers import cwd_at
|
||||
from ..helpers import TestCase
|
||||
|
||||
|
||||
def test_is_keyword():
|
||||
def test_is_keyword(Script):
|
||||
#results = Script('import ', 1, 1, None).goto_definitions()
|
||||
#assert len(results) == 1 and results[0].is_keyword is True
|
||||
results = Script('str', 1, 1, None).goto_definitions()
|
||||
assert len(results) == 1 and results[0].is_keyword is False
|
||||
|
||||
|
||||
def test_basedefinition_type():
|
||||
def test_basedefinition_type(Script, environment):
|
||||
def make_definitions():
|
||||
"""
|
||||
Return a list of definitions for parametrized tests.
|
||||
@@ -43,7 +44,7 @@ def test_basedefinition_type():
|
||||
""")
|
||||
|
||||
definitions = []
|
||||
definitions += names(source)
|
||||
definitions += names(source, environment=environment)
|
||||
|
||||
source += dedent("""
|
||||
variable = sys or C or x or f or g or g() or h""")
|
||||
@@ -66,7 +67,7 @@ def test_basedefinition_type():
|
||||
'generator', 'statement', 'import', 'param')
|
||||
|
||||
|
||||
def test_basedefinition_type_import():
|
||||
def test_basedefinition_type_import(Script):
|
||||
def get_types(source, **kwargs):
|
||||
return set([t.type for t in Script(source, **kwargs).completions()])
|
||||
|
||||
@@ -89,7 +90,7 @@ def test_basedefinition_type_import():
|
||||
assert get_types('import json.tool', column=9) == set(['module'])
|
||||
|
||||
|
||||
def test_function_call_signature_in_doc():
|
||||
def test_function_call_signature_in_doc(Script):
|
||||
defs = Script("""
|
||||
def f(x, y=1, z='a'):
|
||||
pass
|
||||
@@ -98,7 +99,7 @@ def test_function_call_signature_in_doc():
|
||||
assert "f(x, y=1, z='a')" in str(doc)
|
||||
|
||||
|
||||
def test_class_call_signature():
|
||||
def test_class_call_signature(Script):
|
||||
defs = Script("""
|
||||
class Foo:
|
||||
def __init__(self, x, y=1, z='a'):
|
||||
@@ -108,14 +109,14 @@ def test_class_call_signature():
|
||||
assert "Foo(self, x, y=1, z='a')" in str(doc)
|
||||
|
||||
|
||||
def test_position_none_if_builtin():
|
||||
def test_position_none_if_builtin(Script):
|
||||
gotos = Script('import sys; sys.path').goto_assignments()
|
||||
assert gotos[0].line is None
|
||||
assert gotos[0].column is None
|
||||
|
||||
|
||||
@cwd_at('.')
|
||||
def test_completion_docstring():
|
||||
def test_completion_docstring(Script):
|
||||
"""
|
||||
Jedi should follow imports in certain conditions
|
||||
"""
|
||||
@@ -126,7 +127,7 @@ def test_completion_docstring():
|
||||
c = Script('import jedi\njed').completions()[0]
|
||||
assert c.docstring(fast=False) == cleandoc(jedi_doc)
|
||||
|
||||
docstr('import jedi\njedi.Scr', cleandoc(Script.__doc__))
|
||||
docstr('import jedi\njedi.Scr', cleandoc(jedi.Script.__doc__))
|
||||
|
||||
docstr('abcd=3;abcd', '')
|
||||
docstr('"hello"\nabcd=3\nabcd', '')
|
||||
@@ -160,12 +161,12 @@ def test_completion_docstring():
|
||||
)
|
||||
|
||||
|
||||
def test_completion_params():
|
||||
def test_completion_params(Script):
|
||||
c = Script('import string; string.capwords').completions()[0]
|
||||
assert [p.name for p in c.params] == ['s', 'sep']
|
||||
|
||||
|
||||
def test_signature_params():
|
||||
def test_signature_params(Script):
|
||||
def check(defs):
|
||||
params = defs[0].params
|
||||
assert len(params) == 1
|
||||
@@ -182,7 +183,7 @@ def test_signature_params():
|
||||
check(Script(s + '\nbar=foo\nbar').goto_assignments())
|
||||
|
||||
|
||||
def test_param_endings():
|
||||
def test_param_endings(Script):
|
||||
"""
|
||||
Params should be represented without the comma and whitespace they have
|
||||
around them.
|
||||
@@ -192,8 +193,17 @@ def test_param_endings():
|
||||
|
||||
|
||||
class TestIsDefinition(TestCase):
|
||||
@pytest.fixture(autouse=True)
|
||||
def init(self, environment):
|
||||
self.environment = environment
|
||||
|
||||
def _def(self, source, index=-1):
|
||||
return names(dedent(source), references=True, all_scopes=True)[index]
|
||||
return names(
|
||||
dedent(source),
|
||||
references=True,
|
||||
all_scopes=True,
|
||||
environment=self.environment
|
||||
)[index]
|
||||
|
||||
def _bool_is_definitions(self, source):
|
||||
ns = names(dedent(source), references=True, all_scopes=True)
|
||||
@@ -225,8 +235,12 @@ class TestIsDefinition(TestCase):
|
||||
|
||||
|
||||
class TestParent(TestCase):
|
||||
@pytest.fixture(autouse=True)
|
||||
def init(self, Script):
|
||||
self.Script = Script
|
||||
|
||||
def _parent(self, source, line=None, column=None):
|
||||
def_, = Script(dedent(source), line, column).goto_assignments()
|
||||
def_, = self.Script(dedent(source), line, column).goto_assignments()
|
||||
return def_.parent()
|
||||
|
||||
def test_parent(self):
|
||||
@@ -248,20 +262,21 @@ class TestParent(TestCase):
|
||||
assert parent.name == ''
|
||||
assert parent.type == 'module'
|
||||
|
||||
def test_parent_on_completion(self):
|
||||
parent = Script(dedent('''\
|
||||
class Foo():
|
||||
def bar(): pass
|
||||
Foo().bar''')).completions()[0].parent()
|
||||
assert parent.name == 'Foo'
|
||||
assert parent.type == 'class'
|
||||
|
||||
parent = Script('str.join').completions()[0].parent()
|
||||
assert parent.name == 'str'
|
||||
assert parent.type == 'class'
|
||||
def test_parent_on_completion(Script):
|
||||
parent = Script(dedent('''\
|
||||
class Foo():
|
||||
def bar(): pass
|
||||
Foo().bar''')).completions()[0].parent()
|
||||
assert parent.name == 'Foo'
|
||||
assert parent.type == 'class'
|
||||
|
||||
parent = Script('str.join').completions()[0].parent()
|
||||
assert parent.name == 'str'
|
||||
assert parent.type == 'class'
|
||||
|
||||
|
||||
def test_type():
|
||||
def test_type(Script):
|
||||
for c in Script('a = [str()]; a[0].').completions():
|
||||
if c.name == '__class__':
|
||||
assert c.type == 'class'
|
||||
@@ -272,7 +287,8 @@ def test_type():
|
||||
for c in Script('import os; os.path.').completions():
|
||||
assert c.type
|
||||
|
||||
def test_type_II():
|
||||
|
||||
def test_type_II(Script):
|
||||
"""
|
||||
GitHub Issue #833, `keyword`s are seen as `module`s
|
||||
"""
|
||||
@@ -281,100 +297,106 @@ def test_type_II():
|
||||
assert c.type == 'keyword'
|
||||
|
||||
|
||||
class TestGotoAssignments(TestCase):
|
||||
"""
|
||||
This tests the BaseDefinition.goto_assignments function, not the jedi
|
||||
function. They are not really different in functionality, but really
|
||||
different as an implementation.
|
||||
"""
|
||||
def test_repetition(self):
|
||||
defs = names('a = 1; a', references=True, definitions=False)
|
||||
# Repeat on the same variable. Shouldn't change once we're on a
|
||||
# definition.
|
||||
for _ in range(3):
|
||||
assert len(defs) == 1
|
||||
ass = defs[0].goto_assignments()
|
||||
assert ass[0].description == 'a = 1'
|
||||
|
||||
def test_named_params(self):
|
||||
src = """\
|
||||
def foo(a=1, bar=2):
|
||||
pass
|
||||
foo(bar=1)
|
||||
"""
|
||||
bar = names(dedent(src), references=True)[-1]
|
||||
param = bar.goto_assignments()[0]
|
||||
assert (param.line, param.column) == (1, 13)
|
||||
assert param.type == 'param'
|
||||
|
||||
def test_class_call(self):
|
||||
src = 'from threading import Thread; Thread(group=1)'
|
||||
n = names(src, references=True)[-1]
|
||||
assert n.name == 'group'
|
||||
param_def = n.goto_assignments()[0]
|
||||
assert param_def.name == 'group'
|
||||
assert param_def.type == 'param'
|
||||
|
||||
def test_parentheses(self):
|
||||
n = names('("").upper', references=True)[-1]
|
||||
assert n.goto_assignments()[0].name == 'upper'
|
||||
|
||||
def test_import(self):
|
||||
nms = names('from json import load', references=True)
|
||||
assert nms[0].name == 'json'
|
||||
assert nms[0].type == 'module'
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'json'
|
||||
assert n.type == 'module'
|
||||
|
||||
assert nms[1].name == 'load'
|
||||
assert nms[1].type == 'function'
|
||||
n = nms[1].goto_assignments()[0]
|
||||
assert n.name == 'load'
|
||||
assert n.type == 'function'
|
||||
|
||||
nms = names('import os; os.path', references=True)
|
||||
assert nms[0].name == 'os'
|
||||
assert nms[0].type == 'module'
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'os'
|
||||
assert n.type == 'module'
|
||||
|
||||
n = nms[2].goto_assignments()[0]
|
||||
assert n.name == 'path'
|
||||
assert n.type == 'module'
|
||||
|
||||
nms = names('import os.path', references=True)
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'os'
|
||||
assert n.type == 'module'
|
||||
n = nms[1].goto_assignments()[0]
|
||||
# This is very special, normally the name doesn't chance, but since
|
||||
# os.path is a sys.modules hack, it does.
|
||||
assert n.name in ('ntpath', 'posixpath', 'os2emxpath')
|
||||
assert n.type == 'module'
|
||||
|
||||
def test_import_alias(self):
|
||||
nms = names('import json as foo', references=True)
|
||||
assert nms[0].name == 'json'
|
||||
assert nms[0].type == 'module'
|
||||
assert nms[0]._name.tree_name.parent.type == 'dotted_as_name'
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'json'
|
||||
assert n.type == 'module'
|
||||
assert n._name._context.tree_node.type == 'file_input'
|
||||
|
||||
assert nms[1].name == 'foo'
|
||||
assert nms[1].type == 'module'
|
||||
assert nms[1]._name.tree_name.parent.type == 'dotted_as_name'
|
||||
ass = nms[1].goto_assignments()
|
||||
assert len(ass) == 1
|
||||
assert ass[0].name == 'json'
|
||||
assert ass[0].type == 'module'
|
||||
assert ass[0]._name._context.tree_node.type == 'file_input'
|
||||
"""
|
||||
This tests the BaseDefinition.goto_assignments function, not the jedi
|
||||
function. They are not really different in functionality, but really
|
||||
different as an implementation.
|
||||
"""
|
||||
|
||||
|
||||
def test_added_equals_to_params():
|
||||
def test_goto_assignment_repetition(environment):
|
||||
defs = names('a = 1; a', references=True, definitions=False, environment=environment)
|
||||
# Repeat on the same variable. Shouldn't change once we're on a
|
||||
# definition.
|
||||
for _ in range(3):
|
||||
assert len(defs) == 1
|
||||
ass = defs[0].goto_assignments()
|
||||
assert ass[0].description == 'a = 1'
|
||||
|
||||
|
||||
def test_goto_assignments_named_params(environment):
|
||||
src = """\
|
||||
def foo(a=1, bar=2):
|
||||
pass
|
||||
foo(bar=1)
|
||||
"""
|
||||
bar = names(dedent(src), references=True, environment=environment)[-1]
|
||||
param = bar.goto_assignments()[0]
|
||||
assert (param.line, param.column) == (1, 13)
|
||||
assert param.type == 'param'
|
||||
|
||||
|
||||
def test_class_call(environment):
|
||||
src = 'from threading import Thread; Thread(group=1)'
|
||||
n = names(src, references=True, environment=environment)[-1]
|
||||
assert n.name == 'group'
|
||||
param_def = n.goto_assignments()[0]
|
||||
assert param_def.name == 'group'
|
||||
assert param_def.type == 'param'
|
||||
|
||||
|
||||
def test_parentheses(environment):
|
||||
n = names('("").upper', references=True, environment=environment)[-1]
|
||||
assert n.goto_assignments()[0].name == 'upper'
|
||||
|
||||
|
||||
def test_import(environment):
|
||||
nms = names('from json import load', references=True, environment=environment)
|
||||
assert nms[0].name == 'json'
|
||||
assert nms[0].type == 'module'
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'json'
|
||||
assert n.type == 'module'
|
||||
|
||||
assert nms[1].name == 'load'
|
||||
assert nms[1].type == 'function'
|
||||
n = nms[1].goto_assignments()[0]
|
||||
assert n.name == 'load'
|
||||
assert n.type == 'function'
|
||||
|
||||
nms = names('import os; os.path', references=True, environment=environment)
|
||||
assert nms[0].name == 'os'
|
||||
assert nms[0].type == 'module'
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'os'
|
||||
assert n.type == 'module'
|
||||
|
||||
n = nms[2].goto_assignments()[0]
|
||||
assert n.name == 'path'
|
||||
assert n.type == 'module'
|
||||
|
||||
nms = names('import os.path', references=True, environment=environment)
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'os'
|
||||
assert n.type == 'module'
|
||||
n = nms[1].goto_assignments()[0]
|
||||
# This is very special, normally the name doesn't chance, but since
|
||||
# os.path is a sys.modules hack, it does.
|
||||
assert n.name in ('ntpath', 'posixpath', 'os2emxpath')
|
||||
assert n.type == 'module'
|
||||
|
||||
|
||||
def test_import_alias(environment):
|
||||
nms = names('import json as foo', references=True, environment=environment)
|
||||
assert nms[0].name == 'json'
|
||||
assert nms[0].type == 'module'
|
||||
assert nms[0]._name.tree_name.parent.type == 'dotted_as_name'
|
||||
n = nms[0].goto_assignments()[0]
|
||||
assert n.name == 'json'
|
||||
assert n.type == 'module'
|
||||
assert n._name._context.tree_node.type == 'file_input'
|
||||
|
||||
assert nms[1].name == 'foo'
|
||||
assert nms[1].type == 'module'
|
||||
assert nms[1]._name.tree_name.parent.type == 'dotted_as_name'
|
||||
ass = nms[1].goto_assignments()
|
||||
assert len(ass) == 1
|
||||
assert ass[0].name == 'json'
|
||||
assert ass[0].type == 'module'
|
||||
assert ass[0]._name._context.tree_node.type == 'file_input'
|
||||
|
||||
|
||||
def test_added_equals_to_params(Script):
|
||||
def run(rest_source):
|
||||
source = dedent("""
|
||||
def foo(bar, baz):
|
||||
|
||||
Reference in New Issue
Block a user