From ee1f077014c1934b41a6645fa584169f1b167b6a Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 3 Dec 2016 14:32:00 +0100 Subject: [PATCH] Some test refactorings. --- jedi/evaluate/sys_path.py | 27 ++++++------- test/test_evaluate/test_buildout_detection.py | 39 +++++++++++-------- test/test_evaluate/test_precedence.py | 11 ++---- test/test_evaluate/test_representation.py | 8 ++-- test/test_evaluate/test_sys_path.py | 11 +++--- test/test_parser/test_old_fast_parser.py | 4 +- test/test_parser/test_tokenize.py | 2 +- 7 files changed, 53 insertions(+), 49 deletions(-) diff --git a/jedi/evaluate/sys_path.py b/jedi/evaluate/sys_path.py index b35d97280..b16a53c55 100644 --- a/jedi/evaluate/sys_path.py +++ b/jedi/evaluate/sys_path.py @@ -86,7 +86,7 @@ def _execute_code(module_path, code): 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:: @@ -122,11 +122,11 @@ def _paths_from_assignment(evaluator, expr_stmt): from jedi.evaluate.iterable import py__iter__ from jedi.evaluate.precedence import is_string - types = evaluator.eval_element(expr_stmt) - for types in py__iter__(evaluator, types, expr_stmt): - for typ in types: - if is_string(typ): - yield typ.obj + types = module_context.create_context(expr_stmt).eval_node(expr_stmt) + for lazy_context in py__iter__(module_context.evaluator, types, expr_stmt): + for context in lazy_context.infer(): + if is_string(context): + yield context.obj 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()) -def _check_module(evaluator, module_context): +def _check_module(module_context): """ Detect sys.path modifications within module. """ @@ -162,7 +162,7 @@ def get_sys_path_powers(names): if isinstance(n, tree.Name) and n.value == 'path': yield name, power - sys_path = list(evaluator.sys_path) # copy + sys_path = list(module_context.evaluator.sys_path) # copy if isinstance(module_context, CompiledObject): return sys_path @@ -182,7 +182,7 @@ def get_sys_path_powers(names): ) ) 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 @@ -201,7 +201,7 @@ def sys_path_with_modifications(evaluator, module_context): buildout_script_paths = set() - result = _check_module(evaluator, module_context) + result = _check_module(module_context) result += _detect_django_path(path) for buildout_script in _get_buildout_scripts(path): for path in _get_paths_from_buildout_script(evaluator, buildout_script): @@ -225,11 +225,12 @@ def load(buildout_script): return p.module cached = load_parser(buildout_script) - module = cached and cached.module or load(buildout_script) - if not module: + module_node = cached and cached.module or load(buildout_script) + if module_node is None: 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 diff --git a/test/test_evaluate/test_buildout_detection.py b/test/test_evaluate/test_buildout_detection.py index c5c65568a..7e05af0fb 100644 --- a/test/test_evaluate/test_buildout_detection.py +++ b/test/test_evaluate/test_buildout_detection.py @@ -7,11 +7,19 @@ sys_path_with_modifications, _check_module) from jedi.evaluate import Evaluator +from jedi.evaluate.representation import ModuleContext from jedi.parser import ParserWithRecovery, load_grammar 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') def test_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(): - SRC = dedent(u(""" + code = dedent(u(""" class Dummy(object): path = [] d = Dummy() d.path.append('foo')""")) - grammar = load_grammar() - p = ParserWithRecovery(grammar, SRC) - paths = _check_module(Evaluator(grammar), p.module) + + paths = check_module_test(code) assert len(paths) > 0 assert 'foo' not in paths def test_path_from_invalid_sys_path_assignment(): - SRC = dedent(u(""" + code = dedent(u(""" import sys sys.path = 'invalid'""")) - grammar = load_grammar() - p = ParserWithRecovery(grammar, SRC) - paths = _check_module(Evaluator(grammar), p.module) + + paths = check_module_test(code) assert len(paths) > 0 assert 'invalid' not in paths @cwd_at('test/test_evaluate/buildout_project/src/proj_name/') def test_sys_path_with_modifications(): - SRC = dedent(u(""" + code = dedent(u(""" import os """)) + + path = os.path.abspath(os.path.join(os.curdir, 'module_name.py')) grammar = load_grammar() - p = ParserWithRecovery(grammar, SRC) - p.module.path = os.path.abspath(os.path.join(os.curdir, 'module_name.py')) - paths = sys_path_with_modifications(Evaluator(grammar), p.module) + p = ParserWithRecovery(grammar, code, module_path=path) + module_context = ModuleContext(Evaluator(grammar), p.module) + paths = sys_path_with_modifications(module_context.evaluator, module_context) assert '/tmp/.buildout/eggs/important_package.egg' in paths def test_path_from_sys_path_assignment(): - SRC = dedent(u(""" + code = dedent(u(""" #!/usr/bin/python import sys @@ -82,8 +90,7 @@ def test_path_from_sys_path_assignment(): if __name__ == '__main__': sys.exit(important_package.main())""")) - grammar = load_grammar() - p = ParserWithRecovery(grammar, SRC) - paths = _check_module(Evaluator(grammar), p.module) + + paths = check_module_test(code) assert 1 not in paths assert '/home/test/.buildout/eggs/important_package.egg' in paths diff --git a/test/test_evaluate/test_precedence.py b/test/test_evaluate/test_precedence.py index 462b7d4aa..016305dee 100644 --- a/test/test_evaluate/test_precedence.py +++ b/test/test_evaluate/test_precedence.py @@ -1,6 +1,5 @@ -from jedi.parser import load_grammar, Parser -from jedi.evaluate import Evaluator from jedi.evaluate.compiled import CompiledObject +from jedi import Script import pytest @@ -12,9 +11,7 @@ '... == ...' ]) def test_equals(source): - evaluator = Evaluator(load_grammar()) - node = Parser(load_grammar(), source, 'eval_input').get_parsed_node() - results = evaluator.eval_element(node) - assert len(results) == 1 - first = results.pop() + script = Script(source) + node = script._get_module_node().children[0].children[0] + first, = script._get_module().eval_node(node) assert isinstance(first, CompiledObject) and first.obj is True diff --git a/test/test_evaluate/test_representation.py b/test/test_evaluate/test_representation.py index 1d3d5622a..e084bd901 100644 --- a/test/test_evaluate/test_representation.py +++ b/test/test_evaluate/test_representation.py @@ -5,7 +5,7 @@ def get_definition_and_evaluator(source): d = Script(dedent(source)).goto_definitions()[0] - return d._name.parent, d._evaluator + return d._name.parent_context, d._evaluator def test_function_execution(): @@ -22,8 +22,8 @@ def x(): # Now just use the internals of the result (easiest way to get a fully # usable function). # Should return the same result both times. - assert len(evaluator.execute(func)) == 1 - assert len(evaluator.execute(func)) == 1 + assert len(func.execute_evaluated()) == 1 + assert len(func.execute_evaluated()) == 1 def test_class_mro(): @@ -33,4 +33,4 @@ class X(object): X""" cls, evaluator = get_definition_and_evaluator(s) 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'] diff --git a/test/test_evaluate/test_sys_path.py b/test/test_evaluate/test_sys_path.py index f7ce0fabf..98e60b63a 100644 --- a/test/test_evaluate/test_sys_path.py +++ b/test/test_evaluate/test_sys_path.py @@ -4,16 +4,15 @@ import pytest -from jedi._compatibility import unicode -from jedi.parser import ParserWithRecovery, load_grammar -from jedi.evaluate import sys_path, Evaluator +from jedi.evaluate import sys_path +from jedi import Script def test_paths_from_assignment(): def paths(src): - grammar = load_grammar() - stmt = ParserWithRecovery(grammar, unicode(src)).module.statements[0] - return set(sys_path._paths_from_assignment(Evaluator(grammar), stmt)) + script = Script(src) + stmt = script._get_module_node().statements[0] + return set(sys_path._paths_from_assignment(script._get_module(), stmt)) assert paths('sys.path[0:0] = ["a"]') == set(['a']) assert paths('sys.path = ["b", 1, x + 3, y, "c"]') == set(['b', 'c']) diff --git a/test/test_parser/test_old_fast_parser.py b/test/test_parser/test_old_fast_parser.py index 4380b9b1a..f6f9c9f22 100644 --- a/test/test_parser/test_old_fast_parser.py +++ b/test/test_parser/test_old_fast_parser.py @@ -257,7 +257,7 @@ def foo(): """) script = jedi.Script(dedent(source)) - script._get_module().end_pos == (6, 0) + script._get_module().module_node.end_pos == (6, 0) assert script.completions() @@ -275,7 +275,7 @@ def bla(): s = jedi.Script(source) assert s.completions() - assert s._get_module().get_code() == source + assert s._get_module().module_node.get_code() == source def test_round_trip(): diff --git a/test/test_parser/test_tokenize.py b/test/test_parser/test_tokenize.py index e53f85a64..fb33289de 100644 --- a/test/test_parser/test_tokenize.py +++ b/test/test_parser/test_tokenize.py @@ -121,7 +121,7 @@ def test_quoted_strings(self): def test_tokenizer_with_string_literal_backslash(): import jedi 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():