mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 14:34:31 +08:00
Some test refactorings.
This commit is contained in:
@@ -86,7 +86,7 @@ def _execute_code(module_path, code):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_assignment(evaluator, expr_stmt):
|
def _paths_from_assignment(module_context, expr_stmt):
|
||||||
"""
|
"""
|
||||||
Extracts the assigned strings from an assignment that looks as follows::
|
Extracts the assigned strings from an assignment that looks as follows::
|
||||||
|
|
||||||
@@ -122,11 +122,11 @@ def _paths_from_assignment(evaluator, expr_stmt):
|
|||||||
|
|
||||||
from jedi.evaluate.iterable import py__iter__
|
from jedi.evaluate.iterable import py__iter__
|
||||||
from jedi.evaluate.precedence import is_string
|
from jedi.evaluate.precedence import is_string
|
||||||
types = evaluator.eval_element(expr_stmt)
|
types = module_context.create_context(expr_stmt).eval_node(expr_stmt)
|
||||||
for types in py__iter__(evaluator, types, expr_stmt):
|
for lazy_context in py__iter__(module_context.evaluator, types, expr_stmt):
|
||||||
for typ in types:
|
for context in lazy_context.infer():
|
||||||
if is_string(typ):
|
if is_string(context):
|
||||||
yield typ.obj
|
yield context.obj
|
||||||
|
|
||||||
|
|
||||||
def _paths_from_list_modifications(module_path, trailer1, trailer2):
|
def _paths_from_list_modifications(module_path, trailer1, trailer2):
|
||||||
@@ -147,7 +147,7 @@ def _paths_from_list_modifications(module_path, trailer1, trailer2):
|
|||||||
return _execute_code(module_path, arg.get_code())
|
return _execute_code(module_path, arg.get_code())
|
||||||
|
|
||||||
|
|
||||||
def _check_module(evaluator, module_context):
|
def _check_module(module_context):
|
||||||
"""
|
"""
|
||||||
Detect sys.path modifications within module.
|
Detect sys.path modifications within module.
|
||||||
"""
|
"""
|
||||||
@@ -162,7 +162,7 @@ def _check_module(evaluator, module_context):
|
|||||||
if isinstance(n, tree.Name) and n.value == 'path':
|
if isinstance(n, tree.Name) and n.value == 'path':
|
||||||
yield name, power
|
yield name, power
|
||||||
|
|
||||||
sys_path = list(evaluator.sys_path) # copy
|
sys_path = list(module_context.evaluator.sys_path) # copy
|
||||||
if isinstance(module_context, CompiledObject):
|
if isinstance(module_context, CompiledObject):
|
||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ def _check_module(evaluator, module_context):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif name.get_definition().type == 'expr_stmt':
|
elif name.get_definition().type == 'expr_stmt':
|
||||||
sys_path.extend(_paths_from_assignment(evaluator, stmt))
|
sys_path.extend(_paths_from_assignment(module_context, stmt))
|
||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
|
|
||||||
@@ -201,7 +201,7 @@ def sys_path_with_modifications(evaluator, module_context):
|
|||||||
|
|
||||||
buildout_script_paths = set()
|
buildout_script_paths = set()
|
||||||
|
|
||||||
result = _check_module(evaluator, module_context)
|
result = _check_module(module_context)
|
||||||
result += _detect_django_path(path)
|
result += _detect_django_path(path)
|
||||||
for buildout_script in _get_buildout_scripts(path):
|
for buildout_script in _get_buildout_scripts(path):
|
||||||
for path in _get_paths_from_buildout_script(evaluator, buildout_script):
|
for path in _get_paths_from_buildout_script(evaluator, buildout_script):
|
||||||
@@ -225,11 +225,12 @@ def _get_paths_from_buildout_script(evaluator, buildout_script):
|
|||||||
return p.module
|
return p.module
|
||||||
|
|
||||||
cached = load_parser(buildout_script)
|
cached = load_parser(buildout_script)
|
||||||
module = cached and cached.module or load(buildout_script)
|
module_node = cached and cached.module or load(buildout_script)
|
||||||
if not module:
|
if module_node is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
for path in _check_module(evaluator, module):
|
from jedi.evaluate.representation import ModuleContext
|
||||||
|
for path in _check_module(ModuleContext(evaluator, module_node)):
|
||||||
yield path
|
yield path
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,19 @@ from jedi.evaluate.sys_path import (_get_parent_dir_with_file,
|
|||||||
sys_path_with_modifications,
|
sys_path_with_modifications,
|
||||||
_check_module)
|
_check_module)
|
||||||
from jedi.evaluate import Evaluator
|
from jedi.evaluate import Evaluator
|
||||||
|
from jedi.evaluate.representation import ModuleContext
|
||||||
from jedi.parser import ParserWithRecovery, load_grammar
|
from jedi.parser import ParserWithRecovery, load_grammar
|
||||||
|
|
||||||
from ..helpers import cwd_at
|
from ..helpers import cwd_at
|
||||||
|
|
||||||
|
|
||||||
|
def check_module_test(code):
|
||||||
|
grammar = load_grammar()
|
||||||
|
p = ParserWithRecovery(grammar, code)
|
||||||
|
module_context = ModuleContext(Evaluator(grammar), p.module)
|
||||||
|
return _check_module(module_context)
|
||||||
|
|
||||||
|
|
||||||
@cwd_at('test/test_evaluate/buildout_project/src/proj_name')
|
@cwd_at('test/test_evaluate/buildout_project/src/proj_name')
|
||||||
def test_parent_dir_with_file():
|
def test_parent_dir_with_file():
|
||||||
parent = _get_parent_dir_with_file(
|
parent = _get_parent_dir_with_file(
|
||||||
@@ -30,44 +38,44 @@ def test_buildout_detection():
|
|||||||
|
|
||||||
|
|
||||||
def test_append_on_non_sys_path():
|
def test_append_on_non_sys_path():
|
||||||
SRC = dedent(u("""
|
code = dedent(u("""
|
||||||
class Dummy(object):
|
class Dummy(object):
|
||||||
path = []
|
path = []
|
||||||
|
|
||||||
d = Dummy()
|
d = Dummy()
|
||||||
d.path.append('foo')"""))
|
d.path.append('foo')"""))
|
||||||
grammar = load_grammar()
|
|
||||||
p = ParserWithRecovery(grammar, SRC)
|
paths = check_module_test(code)
|
||||||
paths = _check_module(Evaluator(grammar), p.module)
|
|
||||||
assert len(paths) > 0
|
assert len(paths) > 0
|
||||||
assert 'foo' not in paths
|
assert 'foo' not in paths
|
||||||
|
|
||||||
|
|
||||||
def test_path_from_invalid_sys_path_assignment():
|
def test_path_from_invalid_sys_path_assignment():
|
||||||
SRC = dedent(u("""
|
code = dedent(u("""
|
||||||
import sys
|
import sys
|
||||||
sys.path = 'invalid'"""))
|
sys.path = 'invalid'"""))
|
||||||
grammar = load_grammar()
|
|
||||||
p = ParserWithRecovery(grammar, SRC)
|
paths = check_module_test(code)
|
||||||
paths = _check_module(Evaluator(grammar), p.module)
|
|
||||||
assert len(paths) > 0
|
assert len(paths) > 0
|
||||||
assert 'invalid' not in paths
|
assert 'invalid' not in paths
|
||||||
|
|
||||||
|
|
||||||
@cwd_at('test/test_evaluate/buildout_project/src/proj_name/')
|
@cwd_at('test/test_evaluate/buildout_project/src/proj_name/')
|
||||||
def test_sys_path_with_modifications():
|
def test_sys_path_with_modifications():
|
||||||
SRC = dedent(u("""
|
code = dedent(u("""
|
||||||
import os
|
import os
|
||||||
"""))
|
"""))
|
||||||
|
|
||||||
|
path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
|
||||||
grammar = load_grammar()
|
grammar = load_grammar()
|
||||||
p = ParserWithRecovery(grammar, SRC)
|
p = ParserWithRecovery(grammar, code, module_path=path)
|
||||||
p.module.path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
|
module_context = ModuleContext(Evaluator(grammar), p.module)
|
||||||
paths = sys_path_with_modifications(Evaluator(grammar), p.module)
|
paths = sys_path_with_modifications(module_context.evaluator, module_context)
|
||||||
assert '/tmp/.buildout/eggs/important_package.egg' in paths
|
assert '/tmp/.buildout/eggs/important_package.egg' in paths
|
||||||
|
|
||||||
|
|
||||||
def test_path_from_sys_path_assignment():
|
def test_path_from_sys_path_assignment():
|
||||||
SRC = dedent(u("""
|
code = dedent(u("""
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@@ -82,8 +90,7 @@ def test_path_from_sys_path_assignment():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(important_package.main())"""))
|
sys.exit(important_package.main())"""))
|
||||||
grammar = load_grammar()
|
|
||||||
p = ParserWithRecovery(grammar, SRC)
|
paths = check_module_test(code)
|
||||||
paths = _check_module(Evaluator(grammar), p.module)
|
|
||||||
assert 1 not in paths
|
assert 1 not in paths
|
||||||
assert '/home/test/.buildout/eggs/important_package.egg' in paths
|
assert '/home/test/.buildout/eggs/important_package.egg' in paths
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from jedi.parser import load_grammar, Parser
|
|
||||||
from jedi.evaluate import Evaluator
|
|
||||||
from jedi.evaluate.compiled import CompiledObject
|
from jedi.evaluate.compiled import CompiledObject
|
||||||
|
from jedi import Script
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -12,9 +11,7 @@ import pytest
|
|||||||
'... == ...'
|
'... == ...'
|
||||||
])
|
])
|
||||||
def test_equals(source):
|
def test_equals(source):
|
||||||
evaluator = Evaluator(load_grammar())
|
script = Script(source)
|
||||||
node = Parser(load_grammar(), source, 'eval_input').get_parsed_node()
|
node = script._get_module_node().children[0].children[0]
|
||||||
results = evaluator.eval_element(node)
|
first, = script._get_module().eval_node(node)
|
||||||
assert len(results) == 1
|
|
||||||
first = results.pop()
|
|
||||||
assert isinstance(first, CompiledObject) and first.obj is True
|
assert isinstance(first, CompiledObject) and first.obj is True
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from jedi import Script
|
|||||||
|
|
||||||
def get_definition_and_evaluator(source):
|
def get_definition_and_evaluator(source):
|
||||||
d = Script(dedent(source)).goto_definitions()[0]
|
d = Script(dedent(source)).goto_definitions()[0]
|
||||||
return d._name.parent, d._evaluator
|
return d._name.parent_context, d._evaluator
|
||||||
|
|
||||||
|
|
||||||
def test_function_execution():
|
def test_function_execution():
|
||||||
@@ -22,8 +22,8 @@ def test_function_execution():
|
|||||||
# Now just use the internals of the result (easiest way to get a fully
|
# Now just use the internals of the result (easiest way to get a fully
|
||||||
# usable function).
|
# usable function).
|
||||||
# Should return the same result both times.
|
# Should return the same result both times.
|
||||||
assert len(evaluator.execute(func)) == 1
|
assert len(func.execute_evaluated()) == 1
|
||||||
assert len(evaluator.execute(func)) == 1
|
assert len(func.execute_evaluated()) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_class_mro():
|
def test_class_mro():
|
||||||
@@ -33,4 +33,4 @@ def test_class_mro():
|
|||||||
X"""
|
X"""
|
||||||
cls, evaluator = get_definition_and_evaluator(s)
|
cls, evaluator = get_definition_and_evaluator(s)
|
||||||
mro = cls.py__mro__()
|
mro = cls.py__mro__()
|
||||||
assert [str(c.name) for c in mro] == ['X', 'object']
|
assert [c.name.string_name for c in mro] == ['X', 'object']
|
||||||
|
|||||||
@@ -4,16 +4,15 @@ import sys
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from jedi._compatibility import unicode
|
from jedi.evaluate import sys_path
|
||||||
from jedi.parser import ParserWithRecovery, load_grammar
|
from jedi import Script
|
||||||
from jedi.evaluate import sys_path, Evaluator
|
|
||||||
|
|
||||||
|
|
||||||
def test_paths_from_assignment():
|
def test_paths_from_assignment():
|
||||||
def paths(src):
|
def paths(src):
|
||||||
grammar = load_grammar()
|
script = Script(src)
|
||||||
stmt = ParserWithRecovery(grammar, unicode(src)).module.statements[0]
|
stmt = script._get_module_node().statements[0]
|
||||||
return set(sys_path._paths_from_assignment(Evaluator(grammar), stmt))
|
return set(sys_path._paths_from_assignment(script._get_module(), stmt))
|
||||||
|
|
||||||
assert paths('sys.path[0:0] = ["a"]') == set(['a'])
|
assert paths('sys.path[0:0] = ["a"]') == set(['a'])
|
||||||
assert paths('sys.path = ["b", 1, x + 3, y, "c"]') == set(['b', 'c'])
|
assert paths('sys.path = ["b", 1, x + 3, y, "c"]') == set(['b', 'c'])
|
||||||
|
|||||||
@@ -257,7 +257,7 @@ def test_string_literals():
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
script = jedi.Script(dedent(source))
|
script = jedi.Script(dedent(source))
|
||||||
script._get_module().end_pos == (6, 0)
|
script._get_module().module_node.end_pos == (6, 0)
|
||||||
assert script.completions()
|
assert script.completions()
|
||||||
|
|
||||||
|
|
||||||
@@ -275,7 +275,7 @@ def test_decorator_string_issue():
|
|||||||
|
|
||||||
s = jedi.Script(source)
|
s = jedi.Script(source)
|
||||||
assert s.completions()
|
assert s.completions()
|
||||||
assert s._get_module().get_code() == source
|
assert s._get_module().module_node.get_code() == source
|
||||||
|
|
||||||
|
|
||||||
def test_round_trip():
|
def test_round_trip():
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ class TokenTest(unittest.TestCase):
|
|||||||
def test_tokenizer_with_string_literal_backslash():
|
def test_tokenizer_with_string_literal_backslash():
|
||||||
import jedi
|
import jedi
|
||||||
c = jedi.Script("statement = u'foo\\\n'; statement").goto_definitions()
|
c = jedi.Script("statement = u'foo\\\n'; statement").goto_definitions()
|
||||||
assert c[0]._name.parent.obj == 'foo'
|
assert c[0]._name.parent_context.obj == 'foo'
|
||||||
|
|
||||||
|
|
||||||
def test_ur_literals():
|
def test_ur_literals():
|
||||||
|
|||||||
Reference in New Issue
Block a user