forked from VimPlug/jedi
moved api, parser and evaluate test directories to test_api, test_parser...
This commit is contained in:
0
test/test_evaluate/__init__.py
Normal file
0
test/test_evaluate/__init__.py
Normal file
14
test/test_evaluate/absolute_import/local_module.py
Normal file
14
test/test_evaluate/absolute_import/local_module.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""
|
||||
This is a module that imports the *standard library* unittest,
|
||||
despite there being a local "unittest" module. It specifies that it
|
||||
wants the stdlib one with the ``absolute_import`` __future__ import.
|
||||
|
||||
The twisted equivalent of this module is ``twisted.trial._synctest``.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class Assertions(unittest.TestCase):
|
||||
pass
|
||||
14
test/test_evaluate/absolute_import/unittest.py
Normal file
14
test/test_evaluate/absolute_import/unittest.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""
|
||||
This is a module that shadows a builtin (intentionally).
|
||||
|
||||
It imports a local module, which in turn imports stdlib unittest (the
|
||||
name shadowed by this module). If that is properly resolved, there's
|
||||
no problem. However, if jedi doesn't understand absolute_imports, it
|
||||
will get this module again, causing infinite recursion.
|
||||
"""
|
||||
from local_module import Assertions
|
||||
|
||||
|
||||
class TestCase(Assertions):
|
||||
def test(self):
|
||||
self.assertT
|
||||
9
test/test_evaluate/namespace_package/ns1/pkg/__init__.py
Normal file
9
test/test_evaluate/namespace_package/ns1/pkg/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
foo = 'ns1!'
|
||||
|
||||
# this is a namespace package
|
||||
try:
|
||||
import pkg_resources
|
||||
pkg_resources.declare_namespace(__name__)
|
||||
except ImportError:
|
||||
import pkgutil
|
||||
__path__ = pkgutil.extend_path(__path__, __name__)
|
||||
1
test/test_evaluate/namespace_package/ns1/pkg/ns1_file.py
Normal file
1
test/test_evaluate/namespace_package/ns1/pkg/ns1_file.py
Normal file
@@ -0,0 +1 @@
|
||||
foo = 'ns1_file!'
|
||||
@@ -0,0 +1 @@
|
||||
foo = 'ns1_folder!'
|
||||
1
test/test_evaluate/namespace_package/ns2/pkg/ns2_file.py
Normal file
1
test/test_evaluate/namespace_package/ns2/pkg/ns2_file.py
Normal file
@@ -0,0 +1 @@
|
||||
foo = 'ns2_file!'
|
||||
@@ -0,0 +1 @@
|
||||
foo = 'ns2_folder!'
|
||||
@@ -0,0 +1 @@
|
||||
foo = 'nested!'
|
||||
1
test/test_evaluate/not_in_sys_path/not_in_sys_path.py
Normal file
1
test/test_evaluate/not_in_sys_path/not_in_sys_path.py
Normal file
@@ -0,0 +1 @@
|
||||
value = 3
|
||||
@@ -0,0 +1 @@
|
||||
value = 'package'
|
||||
@@ -0,0 +1 @@
|
||||
value = 'package.module'
|
||||
7
test/test_evaluate/not_in_sys_path/pkg/module.py
Normal file
7
test/test_evaluate/not_in_sys_path/pkg/module.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import not_in_sys_path
|
||||
import not_in_sys_path_package
|
||||
from not_in_sys_path_package import module
|
||||
|
||||
not_in_sys_path.value
|
||||
not_in_sys_path_package.value
|
||||
module.value
|
||||
39
test/test_evaluate/test_absolute_import.py
Normal file
39
test/test_evaluate/test_absolute_import.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""
|
||||
Tests ``from __future__ import absolute_import`` (only important for
|
||||
Python 2.X)
|
||||
"""
|
||||
import jedi
|
||||
from jedi.parser import Parser
|
||||
from .. import helpers
|
||||
|
||||
|
||||
def test_explicit_absolute_imports():
|
||||
"""
|
||||
Detect modules with ``from __future__ import absolute_import``.
|
||||
"""
|
||||
parser = Parser("from __future__ import absolute_import", "test.py")
|
||||
assert parser.module.has_explicit_absolute_import
|
||||
|
||||
|
||||
def test_no_explicit_absolute_imports():
|
||||
"""
|
||||
Detect modules without ``from __future__ import absolute_import``.
|
||||
"""
|
||||
parser = Parser("1", "test.py")
|
||||
assert not parser.module.has_explicit_absolute_import
|
||||
|
||||
|
||||
def test_dont_break_imports_without_namespaces():
|
||||
"""
|
||||
The code checking for ``from __future__ import absolute_import`` shouldn't
|
||||
assume that all imports have non-``None`` namespaces.
|
||||
"""
|
||||
src = "from __future__ import absolute_import\nimport xyzzy"
|
||||
parser = Parser(src, "test.py")
|
||||
assert parser.module.has_explicit_absolute_import
|
||||
|
||||
|
||||
@helpers.cwd_at("test/test_evaluate/absolute_import")
|
||||
def test_can_complete_when_shadowing():
|
||||
script = jedi.Script(path="unittest.py")
|
||||
assert script.completions()
|
||||
36
test/test_evaluate/test_compiled.py
Normal file
36
test/test_evaluate/test_compiled.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from jedi._compatibility import builtins
|
||||
from jedi.parser.representation import Function
|
||||
from jedi.evaluate import compiled
|
||||
from jedi.evaluate import Evaluator
|
||||
|
||||
|
||||
def test_simple():
|
||||
e = Evaluator()
|
||||
bltn = compiled.CompiledObject(builtins)
|
||||
obj = compiled.CompiledObject('_str_', bltn)
|
||||
upper = e.find_types(obj, 'upper')
|
||||
assert len(upper) == 1
|
||||
objs = list(e.execute(upper[0]))
|
||||
assert len(objs) == 1
|
||||
assert objs[0].obj is str
|
||||
|
||||
|
||||
def test_fake_loading():
|
||||
assert isinstance(compiled.create(next), Function)
|
||||
|
||||
string = compiled.builtin.get_subscope_by_name('str')
|
||||
from_name = compiled._create_from_name(compiled.builtin, string, '__init__')
|
||||
assert isinstance(from_name, Function)
|
||||
|
||||
|
||||
def test_fake_docstr():
|
||||
assert compiled.create(next).docstr == next.__doc__
|
||||
|
||||
|
||||
def test_parse_function_doc_illegal_docstr():
|
||||
docstr = """
|
||||
test_func(o
|
||||
|
||||
doesn't have a closing bracket.
|
||||
"""
|
||||
assert ('', '') == compiled._parse_function_doc(docstr)
|
||||
61
test/test_evaluate/test_docstring.py
Normal file
61
test/test_evaluate/test_docstring.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""
|
||||
Testing of docstring related issues and especially ``jedi.docstrings``.
|
||||
"""
|
||||
|
||||
import jedi
|
||||
from ..helpers import unittest
|
||||
|
||||
|
||||
class TestDocstring(unittest.TestCase):
|
||||
def test_function_doc(self):
|
||||
defs = jedi.Script("""
|
||||
def func():
|
||||
'''Docstring of `func`.'''
|
||||
func""").goto_definitions()
|
||||
self.assertEqual(defs[0].raw_doc, 'Docstring of `func`.')
|
||||
|
||||
@unittest.skip('need evaluator class for that')
|
||||
def test_attribute_docstring(self):
|
||||
defs = jedi.Script("""
|
||||
x = None
|
||||
'''Docstring of `x`.'''
|
||||
x""").goto_definitions()
|
||||
self.assertEqual(defs[0].raw_doc, 'Docstring of `x`.')
|
||||
|
||||
@unittest.skip('need evaluator class for that')
|
||||
def test_multiple_docstrings(self):
|
||||
defs = jedi.Script("""
|
||||
def func():
|
||||
'''Original docstring.'''
|
||||
x = func
|
||||
'''Docstring of `x`.'''
|
||||
x""").goto_definitions()
|
||||
docs = [d.raw_doc for d in defs]
|
||||
self.assertEqual(docs, ['Original docstring.', 'Docstring of `x`.'])
|
||||
|
||||
def test_completion(self):
|
||||
assert jedi.Script('''
|
||||
class DocstringCompletion():
|
||||
#? []
|
||||
""" asdfas """''').completions()
|
||||
|
||||
def test_docstrings_type_dotted_import(self):
|
||||
s = """
|
||||
def func(arg):
|
||||
'''
|
||||
:type arg: threading.Thread
|
||||
'''
|
||||
arg."""
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'start' in names
|
||||
|
||||
def test_docstrings_type_str(self):
|
||||
s = """
|
||||
def func(arg):
|
||||
'''
|
||||
:type arg: str
|
||||
'''
|
||||
arg."""
|
||||
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'join' in names
|
||||
26
test/test_evaluate/test_imports.py
Normal file
26
test/test_evaluate/test_imports.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import pytest
|
||||
|
||||
import jedi
|
||||
from jedi._compatibility import find_module_py33
|
||||
from ..helpers import cwd_at
|
||||
|
||||
|
||||
@pytest.mark.skipif('sys.version_info < (3,3)')
|
||||
def test_find_module_py33():
|
||||
"""Needs to work like the old find_module."""
|
||||
print(find_module_py33('_io'))
|
||||
assert find_module_py33('_io') == (None, '_io', False)
|
||||
|
||||
|
||||
@cwd_at('test/test_evaluate/not_in_sys_path/pkg')
|
||||
def test_import_not_in_sys_path():
|
||||
"""
|
||||
non-direct imports (not in sys.path)
|
||||
"""
|
||||
a = jedi.Script(path='module.py', line=5).goto_definitions()
|
||||
assert a[0].name == 'int'
|
||||
|
||||
a = jedi.Script(path='module.py', line=6).goto_definitions()
|
||||
assert a[0].name == 'str'
|
||||
a = jedi.Script(path='module.py', line=7).goto_definitions()
|
||||
assert a[0].name == 'str'
|
||||
53
test/test_evaluate/test_namespace_package.py
Normal file
53
test/test_evaluate/test_namespace_package.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import jedi
|
||||
import sys
|
||||
from os.path import dirname, join
|
||||
|
||||
|
||||
def test_namespace_package():
|
||||
sys.path.insert(0, join(dirname(__file__), 'namespace_package/ns1'))
|
||||
sys.path.insert(1, join(dirname(__file__), 'namespace_package/ns2'))
|
||||
try:
|
||||
# goto definition
|
||||
assert jedi.Script('from pkg import ns1_file').goto_definitions()
|
||||
assert jedi.Script('from pkg import ns2_file').goto_definitions()
|
||||
assert not jedi.Script('from pkg import ns3_file').goto_definitions()
|
||||
|
||||
# goto assignment
|
||||
tests = {
|
||||
'from pkg.ns2_folder.nested import foo': 'nested!',
|
||||
'from pkg.ns2_folder import foo': 'ns2_folder!',
|
||||
'from pkg.ns2_file import foo': 'ns2_file!',
|
||||
'from pkg.ns1_folder import foo': 'ns1_folder!',
|
||||
'from pkg.ns1_file import foo': 'ns1_file!',
|
||||
'from pkg import foo': 'ns1!',
|
||||
}
|
||||
for source, solution in tests.items():
|
||||
ass = jedi.Script(source).goto_assignments()
|
||||
assert len(ass) == 1
|
||||
assert ass[0].description == "foo = '%s'" % solution
|
||||
|
||||
# completion
|
||||
completions = jedi.Script('from pkg import ').completions()
|
||||
names = [str(c.name) for c in completions] # str because of unicode
|
||||
compare = ['foo', 'ns1_file', 'ns1_folder', 'ns2_folder', 'ns2_file']
|
||||
# must at least contain these items, other items are not important
|
||||
assert not (set(compare) - set(names))
|
||||
|
||||
tests = {
|
||||
'from pkg import ns2_folder as x': 'ns2_folder!',
|
||||
'from pkg import ns2_file as x': 'ns2_file!',
|
||||
'from pkg.ns2_folder import nested as x': 'nested!',
|
||||
'from pkg import ns1_folder as x': 'ns1_folder!',
|
||||
'from pkg import ns1_file as x': 'ns1_file!',
|
||||
'import pkg as x': 'ns1!',
|
||||
}
|
||||
for source, solution in tests.items():
|
||||
for c in jedi.Script(source + '; x.').completions():
|
||||
if c.name == 'foo':
|
||||
completion = c
|
||||
solution = "statement: foo = '%s'" % solution
|
||||
assert completion.description == solution
|
||||
|
||||
finally:
|
||||
sys.path.pop(0)
|
||||
sys.path.pop(0)
|
||||
Reference in New Issue
Block a user