mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-16 02:27:06 +08:00
Move some more parser tests.
This commit is contained in:
@@ -8,13 +8,11 @@ However the tests might still be relevant for the parser.
|
|||||||
|
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
import jedi
|
|
||||||
from jedi._compatibility import u
|
|
||||||
from jedi.parser.python import parse
|
from jedi.parser.python import parse
|
||||||
|
|
||||||
|
|
||||||
def test_carriage_return_splitting():
|
def test_carriage_return_splitting():
|
||||||
source = u(dedent('''
|
source = dedent('''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -22,39 +20,18 @@ def test_carriage_return_splitting():
|
|||||||
|
|
||||||
class Foo():
|
class Foo():
|
||||||
pass
|
pass
|
||||||
'''))
|
''')
|
||||||
source = source.replace('\n', '\r\n')
|
source = source.replace('\n', '\r\n')
|
||||||
module = parse(source)
|
module = parse(source)
|
||||||
assert [n.value for lst in module.get_used_names().values() for n in lst] == ['Foo']
|
assert [n.value for lst in module.get_used_names().values() for n in lst] == ['Foo']
|
||||||
|
|
||||||
|
|
||||||
def check_p(src, number_parsers_used, number_of_splits=None, number_of_misses=0):
|
def check_p(src, number_parsers_used):
|
||||||
if number_of_splits is None:
|
|
||||||
number_of_splits = number_parsers_used
|
|
||||||
|
|
||||||
module_node = parse(src)
|
module_node = parse(src)
|
||||||
|
|
||||||
assert src == module_node.get_code()
|
assert src == module_node.get_code()
|
||||||
return module_node
|
return module_node
|
||||||
|
|
||||||
|
|
||||||
def test_if():
|
|
||||||
src = dedent('''\
|
|
||||||
def func():
|
|
||||||
x = 3
|
|
||||||
if x:
|
|
||||||
def y():
|
|
||||||
return x
|
|
||||||
return y()
|
|
||||||
|
|
||||||
func()
|
|
||||||
''')
|
|
||||||
|
|
||||||
# Two parsers needed, one for pass and one for the function.
|
|
||||||
check_p(src, 2)
|
|
||||||
assert [d.name for d in jedi.Script(src, 8, 6).goto_definitions()] == ['int']
|
|
||||||
|
|
||||||
|
|
||||||
def test_for():
|
def test_for():
|
||||||
src = dedent("""\
|
src = dedent("""\
|
||||||
for a in [1,2]:
|
for a in [1,2]:
|
||||||
@@ -111,24 +88,6 @@ def test_nested_funcs():
|
|||||||
check_p(src, 3)
|
check_p(src, 3)
|
||||||
|
|
||||||
|
|
||||||
def test_class_and_if():
|
|
||||||
src = dedent("""\
|
|
||||||
class V:
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
if 1:
|
|
||||||
c = 3
|
|
||||||
|
|
||||||
def a_func():
|
|
||||||
return 1
|
|
||||||
|
|
||||||
# COMMENT
|
|
||||||
a_func()""")
|
|
||||||
check_p(src, 5, 5)
|
|
||||||
assert [d.name for d in jedi.Script(src).goto_definitions()] == ['int']
|
|
||||||
|
|
||||||
|
|
||||||
def test_multi_line_params():
|
def test_multi_line_params():
|
||||||
src = dedent("""\
|
src = dedent("""\
|
||||||
def x(a,
|
def x(a,
|
||||||
@@ -226,44 +185,6 @@ def test_additional_indent():
|
|||||||
check_p(source, 2)
|
check_p(source, 2)
|
||||||
|
|
||||||
|
|
||||||
def test_incomplete_function():
|
|
||||||
source = '''return ImportErr'''
|
|
||||||
|
|
||||||
script = jedi.Script(dedent(source), 1, 3)
|
|
||||||
assert script.completions()
|
|
||||||
|
|
||||||
|
|
||||||
def test_string_literals():
|
|
||||||
"""Simplified case of jedi-vim#377."""
|
|
||||||
source = dedent("""
|
|
||||||
x = ur'''
|
|
||||||
|
|
||||||
def foo():
|
|
||||||
pass
|
|
||||||
""")
|
|
||||||
|
|
||||||
script = jedi.Script(dedent(source))
|
|
||||||
assert script._get_module().tree_node.end_pos == (6, 0)
|
|
||||||
assert script.completions()
|
|
||||||
|
|
||||||
|
|
||||||
def test_decorator_string_issue():
|
|
||||||
"""
|
|
||||||
Test case from #589
|
|
||||||
"""
|
|
||||||
source = dedent('''\
|
|
||||||
"""
|
|
||||||
@"""
|
|
||||||
def bla():
|
|
||||||
pass
|
|
||||||
|
|
||||||
bla.''')
|
|
||||||
|
|
||||||
s = jedi.Script(source)
|
|
||||||
assert s.completions()
|
|
||||||
assert s._get_module().tree_node.get_code() == source
|
|
||||||
|
|
||||||
|
|
||||||
def test_round_trip():
|
def test_round_trip():
|
||||||
code = dedent('''
|
code = dedent('''
|
||||||
def x():
|
def x():
|
||||||
|
|||||||
@@ -1,6 +1,52 @@
|
|||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
|
from jedi.parser.python import parse
|
||||||
import jedi
|
import jedi
|
||||||
|
|
||||||
|
|
||||||
def test_form_feed_characters():
|
def test_form_feed_characters():
|
||||||
s = "\f\nclass Test(object):\n pass"
|
s = "\f\nclass Test(object):\n pass"
|
||||||
jedi.Script(s, line=2, column=18).call_signatures()
|
jedi.Script(s, line=2, column=18).call_signatures()
|
||||||
|
|
||||||
|
|
||||||
|
def check_p(src, number_parsers_used):
|
||||||
|
module_node = parse(src)
|
||||||
|
assert src == module_node.get_code()
|
||||||
|
return module_node
|
||||||
|
|
||||||
|
|
||||||
|
def test_if():
|
||||||
|
src = dedent('''\
|
||||||
|
def func():
|
||||||
|
x = 3
|
||||||
|
if x:
|
||||||
|
def y():
|
||||||
|
return x
|
||||||
|
return y()
|
||||||
|
|
||||||
|
func()
|
||||||
|
''')
|
||||||
|
|
||||||
|
# Two parsers needed, one for pass and one for the function.
|
||||||
|
check_p(src, 2)
|
||||||
|
assert [d.name for d in jedi.Script(src, 8, 6).goto_definitions()] == ['int']
|
||||||
|
|
||||||
|
|
||||||
|
def test_class_and_if():
|
||||||
|
src = dedent("""\
|
||||||
|
class V:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if 1:
|
||||||
|
c = 3
|
||||||
|
|
||||||
|
def a_func():
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# COMMENT
|
||||||
|
a_func()""")
|
||||||
|
check_p(src, 5, 5)
|
||||||
|
assert [d.name for d in jedi.Script(src).goto_definitions()] == ['int']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
|
|
||||||
|
|
||||||
@@ -10,3 +12,41 @@ def test_error_correction_with():
|
|||||||
assert len(comps) > 30
|
assert len(comps) > 30
|
||||||
# `open` completions have a closed attribute.
|
# `open` completions have a closed attribute.
|
||||||
assert [1 for c in comps if c.name == 'closed']
|
assert [1 for c in comps if c.name == 'closed']
|
||||||
|
|
||||||
|
|
||||||
|
def test_string_literals():
|
||||||
|
"""Simplified case of jedi-vim#377."""
|
||||||
|
source = dedent("""
|
||||||
|
x = ur'''
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
pass
|
||||||
|
""")
|
||||||
|
|
||||||
|
script = jedi.Script(dedent(source))
|
||||||
|
assert script._get_module().tree_node.end_pos == (6, 0)
|
||||||
|
assert script.completions()
|
||||||
|
|
||||||
|
|
||||||
|
def test_incomplete_function():
|
||||||
|
source = '''return ImportErr'''
|
||||||
|
|
||||||
|
script = jedi.Script(dedent(source), 1, 3)
|
||||||
|
assert script.completions()
|
||||||
|
|
||||||
|
|
||||||
|
def test_decorator_string_issue():
|
||||||
|
"""
|
||||||
|
Test case from #589
|
||||||
|
"""
|
||||||
|
source = dedent('''\
|
||||||
|
"""
|
||||||
|
@"""
|
||||||
|
def bla():
|
||||||
|
pass
|
||||||
|
|
||||||
|
bla.''')
|
||||||
|
|
||||||
|
s = jedi.Script(source)
|
||||||
|
assert s.completions()
|
||||||
|
assert s._get_module().tree_node.get_code() == source
|
||||||
|
|||||||
Reference in New Issue
Block a user