mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 06:24:27 +08:00
Start replacing Script calls with a fixture
This is important to migrate all tests to specific fixtures.
This commit is contained in:
@@ -5,7 +5,7 @@ Test all things related to the ``jedi.api`` module.
|
||||
import os
|
||||
from textwrap import dedent
|
||||
|
||||
from jedi import api
|
||||
from jedi import preload_module
|
||||
from jedi._compatibility import is_py3
|
||||
from pytest import raises
|
||||
from parso import cache
|
||||
@@ -23,19 +23,19 @@ def test_preload_modules():
|
||||
cache.parser_cache.clear()
|
||||
|
||||
try:
|
||||
api.preload_module('sys')
|
||||
preload_module('sys')
|
||||
check_loaded() # compiled (c_builtin) modules shouldn't be in the cache.
|
||||
api.preload_module('types', 'token')
|
||||
preload_module('types', 'token')
|
||||
check_loaded('types', 'token')
|
||||
finally:
|
||||
cache.parser_cache.update(old_cache)
|
||||
|
||||
|
||||
def test_empty_script():
|
||||
assert api.Script('')
|
||||
def test_empty_script(Script):
|
||||
assert Script('')
|
||||
|
||||
|
||||
def test_line_number_errors():
|
||||
def test_line_number_errors(Script):
|
||||
"""
|
||||
Script should raise a ValueError if line/column numbers are not in a
|
||||
valid range.
|
||||
@@ -43,91 +43,91 @@ def test_line_number_errors():
|
||||
s = 'hello'
|
||||
# lines
|
||||
with raises(ValueError):
|
||||
api.Script(s, 2, 0)
|
||||
Script(s, 2, 0)
|
||||
with raises(ValueError):
|
||||
api.Script(s, 0, 0)
|
||||
Script(s, 0, 0)
|
||||
|
||||
# columns
|
||||
with raises(ValueError):
|
||||
api.Script(s, 1, len(s) + 1)
|
||||
Script(s, 1, len(s) + 1)
|
||||
with raises(ValueError):
|
||||
api.Script(s, 1, -1)
|
||||
Script(s, 1, -1)
|
||||
|
||||
# ok
|
||||
api.Script(s, 1, 0)
|
||||
api.Script(s, 1, len(s))
|
||||
Script(s, 1, 0)
|
||||
Script(s, 1, len(s))
|
||||
|
||||
|
||||
def _check_number(source, result='float'):
|
||||
completions = api.Script(source).completions()
|
||||
def _check_number(Script, source, result='float'):
|
||||
completions = Script(source).completions()
|
||||
assert completions[0].parent().name == result
|
||||
|
||||
|
||||
def test_completion_on_number_literals():
|
||||
def test_completion_on_number_literals(Script):
|
||||
# No completions on an int literal (is a float).
|
||||
assert [c.name for c in api.Script('1.').completions()] \
|
||||
assert [c.name for c in Script('1.').completions()] \
|
||||
== ['and', 'if', 'in', 'is', 'not', 'or']
|
||||
|
||||
# Multiple points after an int literal basically mean that there's a float
|
||||
# and a call after that.
|
||||
_check_number('1..')
|
||||
_check_number('1.0.')
|
||||
_check_number(Script, '1..')
|
||||
_check_number(Script, '1.0.')
|
||||
|
||||
# power notation
|
||||
_check_number('1.e14.')
|
||||
_check_number('1.e-3.')
|
||||
_check_number('9e3.')
|
||||
assert api.Script('1.e3..').completions() == []
|
||||
assert api.Script('1.e-13..').completions() == []
|
||||
_check_number(Script, '1.e14.')
|
||||
_check_number(Script, '1.e-3.')
|
||||
_check_number(Script, '9e3.')
|
||||
assert Script('1.e3..').completions() == []
|
||||
assert Script('1.e-13..').completions() == []
|
||||
|
||||
|
||||
def test_completion_on_hex_literals():
|
||||
assert api.Script('0x1..').completions() == []
|
||||
_check_number('0x1.', 'int') # hexdecimal
|
||||
def test_completion_on_hex_literals(Script):
|
||||
assert Script('0x1..').completions() == []
|
||||
_check_number(Script, '0x1.', 'int') # hexdecimal
|
||||
# Completing binary literals doesn't work if they are not actually binary
|
||||
# (invalid statements).
|
||||
assert api.Script('0b2.b').completions() == []
|
||||
_check_number('0b1.', 'int') # binary
|
||||
assert Script('0b2.b').completions() == []
|
||||
_check_number(Script, '0b1.', 'int') # binary
|
||||
|
||||
_check_number('0x2e.', 'int')
|
||||
_check_number('0xE7.', 'int')
|
||||
_check_number('0xEa.', 'int')
|
||||
_check_number(Script, '0x2e.', 'int')
|
||||
_check_number(Script, '0xE7.', 'int')
|
||||
_check_number(Script, '0xEa.', 'int')
|
||||
# theoretically, but people can just check for syntax errors:
|
||||
#assert api.Script('0x.').completions() == []
|
||||
#assert Script('0x.').completions() == []
|
||||
|
||||
|
||||
def test_completion_on_complex_literals():
|
||||
assert api.Script('1j..').completions() == []
|
||||
_check_number('1j.', 'complex')
|
||||
_check_number('44.j.', 'complex')
|
||||
_check_number('4.0j.', 'complex')
|
||||
def test_completion_on_complex_literals(Script):
|
||||
assert Script('1j..').completions() == []
|
||||
_check_number(Script, '1j.', 'complex')
|
||||
_check_number(Script, '44.j.', 'complex')
|
||||
_check_number(Script, '4.0j.', 'complex')
|
||||
# No dot no completion - I thought, but 4j is actually a literall after
|
||||
# which a keyword like or is allowed. Good times, haha!
|
||||
assert (set([c.name for c in api.Script('4j').completions()]) ==
|
||||
assert (set([c.name for c in Script('4j').completions()]) ==
|
||||
set(['if', 'and', 'in', 'is', 'not', 'or']))
|
||||
|
||||
|
||||
def test_goto_assignments_on_non_name():
|
||||
assert api.Script('for').goto_assignments() == []
|
||||
def test_goto_assignments_on_non_name(Script):
|
||||
assert Script('for').goto_assignments() == []
|
||||
|
||||
assert api.Script('assert').goto_assignments() == []
|
||||
assert Script('assert').goto_assignments() == []
|
||||
if is_py3:
|
||||
assert api.Script('True').goto_assignments() == []
|
||||
assert Script('True').goto_assignments() == []
|
||||
else:
|
||||
# In Python 2.7 True is still a name.
|
||||
assert api.Script('True').goto_assignments()[0].description == 'instance True'
|
||||
assert Script('True').goto_assignments()[0].description == 'instance True'
|
||||
|
||||
|
||||
def test_goto_definitions_on_non_name():
|
||||
assert api.Script('import x', column=0).goto_definitions() == []
|
||||
def test_goto_definitions_on_non_name(Script):
|
||||
assert Script('import x', column=0).goto_definitions() == []
|
||||
|
||||
|
||||
def test_goto_definitions_on_generator():
|
||||
def_, = api.Script('def x(): yield 1\ny=x()\ny').goto_definitions()
|
||||
def test_goto_definitions_on_generator(Script):
|
||||
def_, = Script('def x(): yield 1\ny=x()\ny').goto_definitions()
|
||||
assert def_.name == 'generator'
|
||||
|
||||
|
||||
def test_goto_definition_not_multiple():
|
||||
def test_goto_definition_not_multiple(Script):
|
||||
"""
|
||||
There should be only one Definition result if it leads back to the same
|
||||
origin (e.g. instance method)
|
||||
@@ -147,17 +147,17 @@ def test_goto_definition_not_multiple():
|
||||
else:
|
||||
a = A(1)
|
||||
a''')
|
||||
assert len(api.Script(s).goto_definitions()) == 1
|
||||
assert len(Script(s).goto_definitions()) == 1
|
||||
|
||||
|
||||
def test_usage_description():
|
||||
descs = [u.description for u in api.Script("foo = ''; foo").usages()]
|
||||
def test_usage_description(Script):
|
||||
descs = [u.description for u in Script("foo = ''; foo").usages()]
|
||||
assert set(descs) == set(["foo = ''", 'foo'])
|
||||
|
||||
|
||||
def test_get_line_code():
|
||||
def test_get_line_code(Script):
|
||||
def get_line_code(source, line=None, **kwargs):
|
||||
return api.Script(source, line=line).completions()[0].get_line_code(**kwargs)
|
||||
return Script(source, line=line).completions()[0].get_line_code(**kwargs)
|
||||
|
||||
# On builtin
|
||||
assert get_line_code('') == ''
|
||||
@@ -178,22 +178,22 @@ def test_get_line_code():
|
||||
assert get_line_code(code, line=2, after=3, before=3) == code
|
||||
|
||||
|
||||
def test_goto_assignments_follow_imports():
|
||||
def test_goto_assignments_follow_imports(Script):
|
||||
code = dedent("""
|
||||
import inspect
|
||||
inspect.isfunction""")
|
||||
definition, = api.Script(code, column=0).goto_assignments(follow_imports=True)
|
||||
definition, = Script(code, column=0).goto_assignments(follow_imports=True)
|
||||
assert 'inspect.py' in definition.module_path
|
||||
assert (definition.line, definition.column) == (1, 0)
|
||||
|
||||
definition, = api.Script(code).goto_assignments(follow_imports=True)
|
||||
definition, = Script(code).goto_assignments(follow_imports=True)
|
||||
assert 'inspect.py' in definition.module_path
|
||||
assert (definition.line, definition.column) > (1, 0)
|
||||
|
||||
code = '''def param(p): pass\nparam(1)'''
|
||||
start_pos = 1, len('def param(')
|
||||
|
||||
script = api.Script(code, *start_pos)
|
||||
script = Script(code, *start_pos)
|
||||
definition, = script.goto_assignments(follow_imports=True)
|
||||
assert (definition.line, definition.column) == start_pos
|
||||
assert definition.name == 'p'
|
||||
@@ -207,13 +207,13 @@ def test_goto_assignments_follow_imports():
|
||||
definition, = script.goto_assignments()
|
||||
assert (definition.line, definition.column) == start_pos
|
||||
|
||||
d, = api.Script('a = 1\na').goto_assignments(follow_imports=True)
|
||||
d, = Script('a = 1\na').goto_assignments(follow_imports=True)
|
||||
assert d.name == 'a'
|
||||
|
||||
|
||||
def test_goto_module():
|
||||
def test_goto_module(Script):
|
||||
def check(line, expected):
|
||||
script = api.Script(path=path, line=line)
|
||||
script = Script(path=path, line=line)
|
||||
module, = script.goto_assignments()
|
||||
assert module.module_path == expected
|
||||
|
||||
|
||||
Reference in New Issue
Block a user