From bf73fcbed409674cdb92a8f26aee76ad06b12503 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 29 Dec 2017 19:36:05 +0100 Subject: [PATCH] More test_evaluate Script fixtures --- test/test_evaluate/test_extension.py | 12 ++-- test/test_evaluate/test_helpers.py | 4 +- .../test_implicit_namespace_package.py | 18 +++-- test/test_evaluate/test_imports.py | 67 +++++++++---------- test/test_evaluate/test_literals.py | 53 +++++++-------- test/test_evaluate/test_namespace_package.py | 9 ++- test/test_evaluate/test_precedence.py | 6 +- test/test_evaluate/test_pyc.py | 8 --- test/test_evaluate/test_representation.py | 12 ++-- 9 files changed, 89 insertions(+), 100 deletions(-) diff --git a/test/test_evaluate/test_extension.py b/test/test_evaluate/test_extension.py index 83efc20b..bbd422b9 100644 --- a/test/test_evaluate/test_extension.py +++ b/test/test_evaluate/test_extension.py @@ -8,26 +8,26 @@ from ..helpers import cwd_at import pytest -def test_completions(): - s = jedi.Script('import _ctypes; _ctypes.') +def test_completions(Script): + s = Script('import _ctypes; _ctypes.') assert len(s.completions()) >= 15 -def test_call_signatures_extension(): +def test_call_signatures_extension(Script): if os.name == 'nt': func = 'LoadLibrary' params = 1 else: func = 'dlopen' params = 2 - s = jedi.Script('import _ctypes; _ctypes.%s(' % (func,)) + s = Script('import _ctypes; _ctypes.%s(' % (func,)) sigs = s.call_signatures() assert len(sigs) == 1 assert len(sigs[0].params) == params -def test_call_signatures_stdlib(): - s = jedi.Script('import math; math.cos(') +def test_call_signatures_stdlib(Script): + s = Script('import math; math.cos(') sigs = s.call_signatures() assert len(sigs) == 1 assert len(sigs[0].params) == 1 diff --git a/test/test_evaluate/test_helpers.py b/test/test_evaluate/test_helpers.py index 4484c2ba..65dd01cb 100644 --- a/test/test_evaluate/test_helpers.py +++ b/test/test_evaluate/test_helpers.py @@ -4,12 +4,12 @@ from jedi import names from jedi.evaluate import helpers -def test_call_of_leaf_in_brackets(): +def test_call_of_leaf_in_brackets(environment): s = dedent(""" x = 1 type(x) """) - last_x = names(s, references=True, definitions=False)[-1] + last_x = names(s, references=True, definitions=False, environment=environment)[-1] name = last_x._name.tree_name call = helpers.call_of_leaf(name) diff --git a/test/test_evaluate/test_implicit_namespace_package.py b/test/test_evaluate/test_implicit_namespace_package.py index 9bc90987..60f37036 100644 --- a/test/test_evaluate/test_implicit_namespace_package.py +++ b/test/test_evaluate/test_implicit_namespace_package.py @@ -1,16 +1,17 @@ from os.path import dirname, join -import jedi import pytest -@pytest.mark.skipif('sys.version_info[:2] < (3,4)') -def test_implicit_namespace_package(): +def test_implicit_namespace_package(Script, environment): + if environment.version_info < (3, 4): + pytest.skip() + sys_path = [join(dirname(__file__), d) for d in ['implicit_namespace_package/ns1', 'implicit_namespace_package/ns2']] def script_with_path(*args, **kwargs): - return jedi.Script(sys_path=sys_path, *args, **kwargs) + return Script(sys_path=sys_path, *args, **kwargs) # goto definition assert script_with_path('from pkg import ns1_file').goto_definitions() @@ -45,13 +46,16 @@ def test_implicit_namespace_package(): solution = "foo = '%s'" % solution assert completion.description == solution -@pytest.mark.skipif('sys.version_info[:2] < (3,4)') -def test_implicit_nested_namespace_package(): + +def test_implicit_nested_namespace_package(Script, environment): + if environment.version_info < (3, 4): + pytest.skip() + CODE = 'from implicit_nested_namespaces.namespace.pkg.module import CONST' sys_path = [dirname(__file__)] - script = jedi.Script(sys_path=sys_path, source=CODE, line=1, column=61) + script = Script(sys_path=sys_path, source=CODE, line=1, column=61) result = script.goto_definitions() diff --git a/test/test_evaluate/test_imports.py b/test/test_evaluate/test_imports.py index 7baa514d..4382e60e 100644 --- a/test/test_evaluate/test_imports.py +++ b/test/test_evaluate/test_imports.py @@ -8,11 +8,9 @@ import sys import pytest -import jedi from jedi._compatibility import find_module_py33, find_module from ..helpers import cwd_at -from jedi import Script from jedi._compatibility import is_py26 @@ -36,7 +34,7 @@ def test_find_module_not_package(): assert is_package is False -def test_find_module_package_zipped(): +def test_find_module_package_zipped(Script): path = os.path.join(os.path.dirname(__file__), 'zipped_imports/pkg.zip') sys.path.append(path) try: @@ -47,12 +45,11 @@ def test_find_module_package_zipped(): finally: sys.path.pop() - script = jedi.Script('import pkg; pkg.mod', 1, 19, sys_path=[path]) + script = Script('import pkg; pkg.mod', 1, 19, sys_path=[path]) assert len(script.completions()) == 1 -@pytest.mark.skipif('sys.version_info < (2,7)') -def test_find_module_not_package_zipped(): +def test_find_module_not_package_zipped(Script, environment): path = os.path.join(os.path.dirname(__file__), 'zipped_imports/not_pkg.zip') sys.path.append(path) try: @@ -62,25 +59,25 @@ def test_find_module_not_package_zipped(): assert is_package is False finally: sys.path.pop() - script = jedi.Script('import not_pkg; not_pkg.val', 1, 27, sys_path=[path]) + script = Script('import not_pkg; not_pkg.val', 1, 27, sys_path=[path]) assert len(script.completions()) == 1 @cwd_at('test/test_evaluate/not_in_sys_path/pkg') -def test_import_not_in_sys_path(): +def test_import_not_in_sys_path(Script): """ non-direct imports (not in sys.path) """ - a = jedi.Script(path='module.py', line=5).goto_definitions() + a = Script(path='module.py', line=5).goto_definitions() assert a[0].name == 'int' - a = jedi.Script(path='module.py', line=6).goto_definitions() + a = Script(path='module.py', line=6).goto_definitions() assert a[0].name == 'str' - a = jedi.Script(path='module.py', line=7).goto_definitions() + a = Script(path='module.py', line=7).goto_definitions() assert a[0].name == 'str' -@pytest.mark.parametrize("script,name", [ +@pytest.mark.parametrize("code,name", [ ("from flask.ext import foo; foo.", "Foo"), # flask_foo.py ("from flask.ext import bar; bar.", "Bar"), # flaskext/bar.py ("from flask.ext import baz; baz.", "Baz"), # flask_baz/__init__.py @@ -94,36 +91,36 @@ def test_import_not_in_sys_path(): pytest.mark.xfail(("import flask.ext.baz; flask.ext.baz.", "Foo")), pytest.mark.xfail(("import flask.ext.moo; flask.ext.moo.", "Foo")), ]) -def test_flask_ext(script, name): +def test_flask_ext(Script, code, name): """flask.ext.foo is really imported from flaskext.foo or flask_foo. """ path = os.path.join(os.path.dirname(__file__), 'flask-site-packages') - completions = jedi.Script(script, sys_path=[path]).completions() + completions = Script(code, sys_path=[path]).completions() assert name in [c.name for c in completions] @cwd_at('test/test_evaluate/') -def test_not_importable_file(): +def test_not_importable_file(Script): src = 'import not_importable_file as x; x.' - assert not jedi.Script(src, path='example.py').completions() + assert not Script(src, path='example.py').completions() -def test_import_unique(): +def test_import_unique(Script): src = "import os; os.path" - defs = jedi.Script(src, path='example.py').goto_definitions() + defs = Script(src, path='example.py').goto_definitions() parent_contexts = [d._name._context for d in defs] assert len(parent_contexts) == len(set(parent_contexts)) -def test_cache_works_with_sys_path_param(tmpdir): +def test_cache_works_with_sys_path_param(Script, tmpdir): foo_path = tmpdir.join('foo') bar_path = tmpdir.join('bar') foo_path.join('module.py').write('foo = 123', ensure=True) bar_path.join('module.py').write('bar = 123', ensure=True) - foo_completions = jedi.Script('import module; module.', - sys_path=[foo_path.strpath]).completions() - bar_completions = jedi.Script('import module; module.', - sys_path=[bar_path.strpath]).completions() + foo_completions = Script('import module; module.', + sys_path=[foo_path.strpath]).completions() + bar_completions = Script('import module; module.', + sys_path=[bar_path.strpath]).completions() assert 'foo' in [c.name for c in foo_completions] assert 'bar' not in [c.name for c in foo_completions] @@ -131,9 +128,9 @@ def test_cache_works_with_sys_path_param(tmpdir): assert 'foo' not in [c.name for c in bar_completions] -def test_import_completion_docstring(): +def test_import_completion_docstring(Script): import abc - s = jedi.Script('"""test"""\nimport ab') + s = Script('"""test"""\nimport ab') completions = s.completions() assert len(completions) == 1 assert completions[0].docstring(fast=False) == abc.__doc__ @@ -143,13 +140,13 @@ def test_import_completion_docstring(): assert completions[0].docstring() == '' -def test_goto_definition_on_import(): +def test_goto_definition_on_import(Script): assert Script("import sys_blabla", 1, 8).goto_definitions() == [] assert len(Script("import sys", 1, 8).goto_definitions()) == 1 @cwd_at('jedi') -def test_complete_on_empty_import(): +def test_complete_on_empty_import(Script): assert Script("from datetime import").completions()[0].name == 'import' # should just list the files in the directory assert 10 < len(Script("from .", path='whatever.py').completions()) < 30 @@ -174,7 +171,7 @@ def test_complete_on_empty_import(): assert Script("from datetime import ").completions() -def test_imports_on_global_namespace_without_path(): +def test_imports_on_global_namespace_without_path(Script): """If the path is None, there shouldn't be any import problem""" completions = Script("import operator").completions() assert [c.name for c in completions] == ['operator'] @@ -188,7 +185,7 @@ def test_imports_on_global_namespace_without_path(): assert [c.name for c in completions] == ['keyword'] -def test_named_import(): +def test_named_import(Script): """named import - jedi-vim issue #8""" s = "import time as dt" assert len(Script(s, 1, 15, '/').goto_definitions()) == 1 @@ -196,14 +193,14 @@ def test_named_import(): @pytest.mark.skipif('True', reason='The nested import stuff is still very messy.') -def test_goto_following_on_imports(): +def test_goto_following_on_imports(Script): s = "import multiprocessing.dummy; multiprocessing.dummy" g = Script(s).goto_assignments() assert len(g) == 1 assert (g[0].line, g[0].column) != (0, 0) -def test_os_after_from(): +def test_os_after_from(Script): def check(source, result, column=None): completions = Script(source, column=column).completions() assert [c.name for c in completions] == result @@ -217,9 +214,9 @@ def test_os_after_from(): check('from os \\\n', ['import']) -def test_os_issues(): +def test_os_issues(Script): def import_names(*args, **kwargs): - return [d.name for d in jedi.Script(*args, **kwargs).completions()] + return [d.name for d in Script(*args, **kwargs).completions()] # Github issue #759 s = 'import os, s' @@ -235,9 +232,9 @@ def test_os_issues(): assert 'path' in import_names(s, column=len(s) - 3) -def test_path_issues(): +def test_path_issues(Script): """ See pull request #684 for details. """ source = '''from datetime import ''' - assert jedi.Script(source).completions() + assert Script(source).completions() diff --git a/test/test_evaluate/test_literals.py b/test/test_evaluate/test_literals.py index 491ca520..17039535 100644 --- a/test/test_evaluate/test_literals.py +++ b/test/test_evaluate/test_literals.py @@ -1,41 +1,40 @@ import pytest -import jedi -from jedi._compatibility import py_version, unicode - -def _eval_literal(code): - def_, = jedi.Script(code).goto_definitions() +def _eval_literal(Script, code): + def_, = Script(code).goto_definitions() return def_._name._context.get_safe_value() -@pytest.mark.skipif('sys.version_info[:2] < (3, 6)') -def test_f_strings(): +def test_f_strings(Script, environment): """ f literals are not really supported in Jedi. They just get ignored and an empty string is returned. """ - assert _eval_literal('f"asdf"') == '' - assert _eval_literal('f"{asdf}"') == '' - assert _eval_literal('F"{asdf}"') == '' - assert _eval_literal('rF"{asdf}"') == '' + if environment.version_info < (3, 6): + pytest.skip() + + assert _eval_literal(Script, 'f"asdf"') == '' + assert _eval_literal(Script, 'f"{asdf}"') == '' + assert _eval_literal(Script, 'F"{asdf}"') == '' + assert _eval_literal(Script, 'rF"{asdf}"') == '' -def test_rb_strings(): - assert _eval_literal('br"asdf"') == b'asdf' - obj = _eval_literal('rb"asdf"') - if py_version < 33: - # rb is not valid in Python 2. Due to error recovery we just get a - # string. - assert obj == 'asdf' - else: - assert obj == b'asdf' +def test_rb_strings(Script, environment): + assert _eval_literal(Script, 'br"asdf"') == b'asdf' + obj = _eval_literal(Script, 'rb"asdf"') + + # rb is not valid in Python 2. Due to error recovery we just get a + # string. + assert obj == b'asdf' -@pytest.mark.skipif('sys.version_info[:2] < (3, 6)') -def test_thousand_separators(): - assert _eval_literal('1_2_3') == 123 - assert _eval_literal('123_456_789') == 123456789 - assert _eval_literal('0x3_4') == 52 - assert _eval_literal('0b1_0') == 2 - assert _eval_literal('0o1_0') == 8 +def test_thousand_separators(Script, environment): + if environment.version_info < (3, 6): + pytest.skip() + + assert _eval_literal(Script, '1_2_3') == 123 + assert _eval_literal(Script, '123_456_789') == 123456789 + assert _eval_literal(Script, '0x3_4') == 52 + assert _eval_literal(Script, '0b1_0') == 2 + assert _eval_literal(Script, '0o1_0') == 8 diff --git a/test/test_evaluate/test_namespace_package.py b/test/test_evaluate/test_namespace_package.py index 9d3aeb75..09ac363c 100644 --- a/test/test_evaluate/test_namespace_package.py +++ b/test/test_evaluate/test_namespace_package.py @@ -1,13 +1,12 @@ -import jedi from os.path import dirname, join -def test_namespace_package(): +def test_namespace_package(Script): sys_path = [join(dirname(__file__), d) for d in ['namespace_package/ns1', 'namespace_package/ns2']] def script_with_path(*args, **kwargs): - return jedi.Script(sys_path=sys_path, *args, **kwargs) + return Script(sys_path=sys_path, *args, **kwargs) # goto definition assert script_with_path('from pkg import ns1_file').goto_definitions() @@ -53,12 +52,12 @@ def test_namespace_package(): assert completion.description == solution -def test_nested_namespace_package(): +def test_nested_namespace_package(Script): code = 'from nested_namespaces.namespace.pkg import CONST' sys_path = [dirname(__file__)] - script = jedi.Script(sys_path=sys_path, source=code, line=1, column=45) + script = Script(sys_path=sys_path, source=code, line=1, column=45) result = script.goto_definitions() diff --git a/test/test_evaluate/test_precedence.py b/test/test_evaluate/test_precedence.py index 02dbbecb..fe3d4bc5 100644 --- a/test/test_evaluate/test_precedence.py +++ b/test/test_evaluate/test_precedence.py @@ -1,16 +1,16 @@ from jedi.evaluate.compiled import CompiledObject -from jedi import Script import pytest -@pytest.mark.skipif('sys.version_info[0] < 3') # Ellipsis does not exists in 2 @pytest.mark.parametrize('source', [ '1 == 1', '1.0 == 1', '... == ...' ]) -def test_equals(source): +def test_equals(Script, environment, source): + if environment.version_info.major < 3: + pytest.skip("Ellipsis does not exists in 2") script = Script(source) node = script._get_module_node().children[0] first, = script._get_module().eval_node(node) diff --git a/test/test_evaluate/test_pyc.py b/test/test_evaluate/test_pyc.py index c101da13..de6286a1 100644 --- a/test/test_evaluate/test_pyc.py +++ b/test/test_evaluate/test_pyc.py @@ -10,8 +10,6 @@ import os import shutil import sys -import pytest - import jedi from ..helpers import cwd_at @@ -45,8 +43,6 @@ def generate_pyc(): shutil.copy(os.path.join("dummy_package/__pycache__", f), dst) -# Python 2.6 does not necessarily come with `compileall.compile_file`. -@pytest.mark.skipif("sys.version_info > (2,6)") @cwd_at('test/test_evaluate') def test_pyc(): """ @@ -58,7 +54,3 @@ def test_pyc(): assert len(s.completions()) >= 2 finally: shutil.rmtree("dummy_package") - - -if __name__ == "__main__": - test_pyc() diff --git a/test/test_evaluate/test_representation.py b/test/test_evaluate/test_representation.py index 1323a9cb..61130230 100644 --- a/test/test_evaluate/test_representation.py +++ b/test/test_evaluate/test_representation.py @@ -1,14 +1,12 @@ from textwrap import dedent -from jedi import Script - -def get_definition_and_evaluator(source): +def get_definition_and_evaluator(Script, source): first, = Script(dedent(source)).goto_definitions() return first._name._context, first._evaluator -def test_function_execution(): +def test_function_execution(Script): """ We've been having an issue of a mutable list that was changed inside the function execution. Test if an execution always returns the same result. @@ -18,7 +16,7 @@ def test_function_execution(): def x(): return str() x""" - func, evaluator = get_definition_and_evaluator(s) + func, evaluator = get_definition_and_evaluator(Script, s) # Now just use the internals of the result (easiest way to get a fully # usable function). # Should return the same result both times. @@ -26,11 +24,11 @@ def test_function_execution(): assert len(func.execute_evaluated()) == 1 -def test_class_mro(): +def test_class_mro(Script): s = """ class X(object): pass X""" - cls, evaluator = get_definition_and_evaluator(s) + cls, evaluator = get_definition_and_evaluator(Script, s) mro = cls.py__mro__() assert [c.name.string_name for c in mro] == ['X', 'object']