forked from VimPlug/jedi
Move some parser tests.
This commit is contained in:
@@ -4,13 +4,10 @@ from textwrap import dedent
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import jedi
|
|
||||||
from jedi._compatibility import u, is_py3
|
from jedi._compatibility import u, is_py3
|
||||||
from jedi.parser.python import parse, load_grammar
|
from jedi.parser.python import parse, load_grammar
|
||||||
from jedi.parser.python import tree
|
from jedi.parser.python import tree
|
||||||
from jedi.common import splitlines
|
from jedi.common import splitlines
|
||||||
from jedi.parser_utils import get_statement_of_position, \
|
|
||||||
clean_scope_docstring, safe_literal_eval
|
|
||||||
|
|
||||||
|
|
||||||
def test_basic_parsing():
|
def test_basic_parsing():
|
||||||
@@ -23,53 +20,6 @@ def test_basic_parsing():
|
|||||||
compare('def x(a, b:3): pass\n')
|
compare('def x(a, b:3): pass\n')
|
||||||
compare('assert foo\n')
|
compare('assert foo\n')
|
||||||
|
|
||||||
def test_user_statement_on_import():
|
|
||||||
"""github #285"""
|
|
||||||
s = "from datetime import (\n" \
|
|
||||||
" time)"
|
|
||||||
|
|
||||||
for pos in [(2, 1), (2, 4)]:
|
|
||||||
p = parse(s)
|
|
||||||
stmt = get_statement_of_position(p, pos)
|
|
||||||
assert isinstance(stmt, tree.Import)
|
|
||||||
assert [n.value for n in stmt.get_defined_names()] == ['time']
|
|
||||||
|
|
||||||
|
|
||||||
class TestCallAndName():
|
|
||||||
def get_call(self, source):
|
|
||||||
# Get the simple_stmt and then the first one.
|
|
||||||
simple_stmt = parse(source).children[0]
|
|
||||||
return simple_stmt.children[0]
|
|
||||||
|
|
||||||
def test_name_and_call_positions(self):
|
|
||||||
name = self.get_call('name\nsomething_else')
|
|
||||||
assert name.value == 'name'
|
|
||||||
assert name.start_pos == (1, 0)
|
|
||||||
assert name.end_pos == (1, 4)
|
|
||||||
|
|
||||||
leaf = self.get_call('1.0\n')
|
|
||||||
assert leaf.value == '1.0'
|
|
||||||
assert safe_literal_eval(leaf.value) == 1.0
|
|
||||||
assert leaf.start_pos == (1, 0)
|
|
||||||
assert leaf.end_pos == (1, 3)
|
|
||||||
|
|
||||||
def test_call_type(self):
|
|
||||||
call = self.get_call('hello')
|
|
||||||
assert isinstance(call, tree.Name)
|
|
||||||
|
|
||||||
def test_literal_type(self):
|
|
||||||
literal = self.get_call('1.0')
|
|
||||||
assert isinstance(literal, tree.Literal)
|
|
||||||
assert type(safe_literal_eval(literal.value)) == float
|
|
||||||
|
|
||||||
literal = self.get_call('1')
|
|
||||||
assert isinstance(literal, tree.Literal)
|
|
||||||
assert type(safe_literal_eval(literal.value)) == int
|
|
||||||
|
|
||||||
literal = self.get_call('"hello"')
|
|
||||||
assert isinstance(literal, tree.Literal)
|
|
||||||
assert safe_literal_eval(literal.value) == 'hello'
|
|
||||||
|
|
||||||
|
|
||||||
class TestSubscopes():
|
class TestSubscopes():
|
||||||
def get_sub(self, source):
|
def get_sub(self, source):
|
||||||
@@ -135,33 +85,6 @@ def test_incomplete_list_comprehension():
|
|||||||
['error_node', 'error_node', 'newline', 'endmarker']
|
['error_node', 'error_node', 'newline', 'endmarker']
|
||||||
|
|
||||||
|
|
||||||
def test_hex_values_in_docstring():
|
|
||||||
source = r'''
|
|
||||||
def foo(object):
|
|
||||||
"""
|
|
||||||
\xff
|
|
||||||
"""
|
|
||||||
return 1
|
|
||||||
'''
|
|
||||||
|
|
||||||
doc = clean_scope_docstring(next(parse(source).iter_funcdefs()))
|
|
||||||
if is_py3:
|
|
||||||
assert doc == '\xff'
|
|
||||||
else:
|
|
||||||
assert doc == u('<EFBFBD>')
|
|
||||||
|
|
||||||
|
|
||||||
def test_error_correction_with():
|
|
||||||
source = """
|
|
||||||
with open() as f:
|
|
||||||
try:
|
|
||||||
f."""
|
|
||||||
comps = jedi.Script(source).completions()
|
|
||||||
assert len(comps) > 30
|
|
||||||
# `open` completions have a closed attribute.
|
|
||||||
assert [1 for c in comps if c.name == 'closed']
|
|
||||||
|
|
||||||
|
|
||||||
def test_newline_positions():
|
def test_newline_positions():
|
||||||
endmarker = parse('a\n').children[-1]
|
endmarker = parse('a\n').children[-1]
|
||||||
assert endmarker.end_pos == (2, 0)
|
assert endmarker.end_pos == (2, 0)
|
||||||
|
|||||||
12
test/test_parso_integration/test_error_correction.py
Normal file
12
test/test_parso_integration/test_error_correction.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import jedi
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_correction_with():
|
||||||
|
source = """
|
||||||
|
with open() as f:
|
||||||
|
try:
|
||||||
|
f."""
|
||||||
|
comps = jedi.Script(source).completions()
|
||||||
|
assert len(comps) > 30
|
||||||
|
# `open` completions have a closed attribute.
|
||||||
|
assert [1 for c in comps if c.name == 'closed']
|
||||||
69
test/test_parso_integration/test_parser_utils.py
Normal file
69
test/test_parso_integration/test_parser_utils.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
from jedi._compatibility import u, is_py3
|
||||||
|
from jedi.parser_utils import get_statement_of_position, \
|
||||||
|
clean_scope_docstring, safe_literal_eval
|
||||||
|
from jedi.parser.python import parse
|
||||||
|
from jedi.parser.python import tree
|
||||||
|
|
||||||
|
|
||||||
|
class TestCallAndName():
|
||||||
|
def get_call(self, source):
|
||||||
|
# Get the simple_stmt and then the first one.
|
||||||
|
simple_stmt = parse(source).children[0]
|
||||||
|
return simple_stmt.children[0]
|
||||||
|
|
||||||
|
def test_name_and_call_positions(self):
|
||||||
|
name = self.get_call('name\nsomething_else')
|
||||||
|
assert name.value == 'name'
|
||||||
|
assert name.start_pos == (1, 0)
|
||||||
|
assert name.end_pos == (1, 4)
|
||||||
|
|
||||||
|
leaf = self.get_call('1.0\n')
|
||||||
|
assert leaf.value == '1.0'
|
||||||
|
assert safe_literal_eval(leaf.value) == 1.0
|
||||||
|
assert leaf.start_pos == (1, 0)
|
||||||
|
assert leaf.end_pos == (1, 3)
|
||||||
|
|
||||||
|
def test_call_type(self):
|
||||||
|
call = self.get_call('hello')
|
||||||
|
assert isinstance(call, tree.Name)
|
||||||
|
|
||||||
|
def test_literal_type(self):
|
||||||
|
literal = self.get_call('1.0')
|
||||||
|
assert isinstance(literal, tree.Literal)
|
||||||
|
assert type(safe_literal_eval(literal.value)) == float
|
||||||
|
|
||||||
|
literal = self.get_call('1')
|
||||||
|
assert isinstance(literal, tree.Literal)
|
||||||
|
assert type(safe_literal_eval(literal.value)) == int
|
||||||
|
|
||||||
|
literal = self.get_call('"hello"')
|
||||||
|
assert isinstance(literal, tree.Literal)
|
||||||
|
assert safe_literal_eval(literal.value) == 'hello'
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_statement_on_import():
|
||||||
|
"""github #285"""
|
||||||
|
s = "from datetime import (\n" \
|
||||||
|
" time)"
|
||||||
|
|
||||||
|
for pos in [(2, 1), (2, 4)]:
|
||||||
|
p = parse(s)
|
||||||
|
stmt = get_statement_of_position(p, pos)
|
||||||
|
assert isinstance(stmt, tree.Import)
|
||||||
|
assert [n.value for n in stmt.get_defined_names()] == ['time']
|
||||||
|
|
||||||
|
|
||||||
|
def test_hex_values_in_docstring():
|
||||||
|
source = r'''
|
||||||
|
def foo(object):
|
||||||
|
"""
|
||||||
|
\xff
|
||||||
|
"""
|
||||||
|
return 1
|
||||||
|
'''
|
||||||
|
|
||||||
|
doc = clean_scope_docstring(next(parse(source).iter_funcdefs()))
|
||||||
|
if is_py3:
|
||||||
|
assert doc == '\xff'
|
||||||
|
else:
|
||||||
|
assert doc == u('<EFBFBD>')
|
||||||
Reference in New Issue
Block a user