mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 06:24:27 +08:00
Cleanup the docstring tests
This commit is contained in:
@@ -13,128 +13,138 @@ except ImportError:
|
|||||||
numpydoc_unavailable = True
|
numpydoc_unavailable = True
|
||||||
else:
|
else:
|
||||||
numpydoc_unavailable = False
|
numpydoc_unavailable = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import numpy
|
import numpy # NOQA
|
||||||
except ImportError:
|
except ImportError:
|
||||||
numpy_unavailable = True
|
numpy_unavailable = True
|
||||||
else:
|
else:
|
||||||
numpy_unavailable = False
|
numpy_unavailable = False
|
||||||
|
|
||||||
|
|
||||||
class TestDocstring(unittest.TestCase):
|
def test_function_doc(Script):
|
||||||
def test_function_doc(self):
|
defs = Script("""
|
||||||
defs = jedi.Script("""
|
def func():
|
||||||
def func():
|
'''Docstring of `func`.'''
|
||||||
'''Docstring of `func`.'''
|
func""").goto_definitions()
|
||||||
func""").goto_definitions()
|
assert defs[0].docstring() == 'func()\n\nDocstring of `func`.'
|
||||||
self.assertEqual(defs[0].docstring(), 'func()\n\nDocstring of `func`.')
|
|
||||||
|
|
||||||
def test_class_doc(self):
|
|
||||||
defs = jedi.Script("""
|
|
||||||
class TestClass():
|
|
||||||
'''Docstring of `TestClass`.'''
|
|
||||||
TestClass""").goto_definitions()
|
|
||||||
self.assertEqual(defs[0].docstring(), 'Docstring of `TestClass`.')
|
|
||||||
|
|
||||||
def test_instance_doc(self):
|
|
||||||
defs = jedi.Script("""
|
|
||||||
class TestClass():
|
|
||||||
'''Docstring of `TestClass`.'''
|
|
||||||
tc = TestClass()
|
|
||||||
tc""").goto_definitions()
|
|
||||||
self.assertEqual(defs[0].docstring(), 'Docstring of `TestClass`.')
|
|
||||||
|
|
||||||
@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].docstring(), '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.docstring() 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: random.Random
|
|
||||||
'''
|
|
||||||
arg."""
|
|
||||||
names = [c.name for c in jedi.Script(s).completions()]
|
|
||||||
assert 'seed' in names
|
|
||||||
|
|
||||||
def test_docstrings_param_type(self):
|
|
||||||
s = """
|
|
||||||
def func(arg):
|
|
||||||
'''
|
|
||||||
:param str arg: some description
|
|
||||||
'''
|
|
||||||
arg."""
|
|
||||||
names = [c.name for c in jedi.Script(s).completions()]
|
|
||||||
assert 'join' 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
|
|
||||||
|
|
||||||
def test_docstring_instance(self):
|
|
||||||
# The types hint that it's a certain kind
|
|
||||||
s = dedent("""
|
|
||||||
class A:
|
|
||||||
def __init__(self,a):
|
|
||||||
'''
|
|
||||||
:type a: threading.Thread
|
|
||||||
'''
|
|
||||||
|
|
||||||
if a is not None:
|
|
||||||
a.start()
|
|
||||||
|
|
||||||
self.a = a
|
|
||||||
|
|
||||||
|
|
||||||
def method_b(c):
|
def test_class_doc(Script):
|
||||||
|
defs = Script("""
|
||||||
|
class TestClass():
|
||||||
|
'''Docstring of `TestClass`.'''
|
||||||
|
TestClass""").goto_definitions()
|
||||||
|
assert defs[0].docstring() == 'Docstring of `TestClass`.'
|
||||||
|
|
||||||
|
|
||||||
|
def test_instance_doc(Script):
|
||||||
|
defs = Script("""
|
||||||
|
class TestClass():
|
||||||
|
'''Docstring of `TestClass`.'''
|
||||||
|
tc = TestClass()
|
||||||
|
tc""").goto_definitions()
|
||||||
|
assert defs[0].docstring() == 'Docstring of `TestClass`.'
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skip('need evaluator class for that')
|
||||||
|
def test_attribute_docstring(Script):
|
||||||
|
defs = Script("""
|
||||||
|
x = None
|
||||||
|
'''Docstring of `x`.'''
|
||||||
|
x""").goto_definitions()
|
||||||
|
assert defs[0].docstring() == 'Docstring of `x`.'
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skip('need evaluator class for that')
|
||||||
|
def test_multiple_docstrings(Script):
|
||||||
|
defs = Script("""
|
||||||
|
def func():
|
||||||
|
'''Original docstring.'''
|
||||||
|
x = func
|
||||||
|
'''Docstring of `x`.'''
|
||||||
|
x""").goto_definitions()
|
||||||
|
docs = [d.docstring() for d in defs]
|
||||||
|
assert docs == ['Original docstring.', 'Docstring of `x`.']
|
||||||
|
|
||||||
|
|
||||||
|
def test_completion(Script):
|
||||||
|
assert Script('''
|
||||||
|
class DocstringCompletion():
|
||||||
|
#? []
|
||||||
|
""" asdfas """''').completions()
|
||||||
|
|
||||||
|
|
||||||
|
def test_docstrings_type_dotted_import(Script):
|
||||||
|
s = """
|
||||||
|
def func(arg):
|
||||||
'''
|
'''
|
||||||
:type c: A
|
:type arg: random.Random
|
||||||
|
'''
|
||||||
|
arg."""
|
||||||
|
names = [c.name for c in Script(s).completions()]
|
||||||
|
assert 'seed' in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_docstrings_param_type(Script):
|
||||||
|
s = """
|
||||||
|
def func(arg):
|
||||||
|
'''
|
||||||
|
:param str arg: some description
|
||||||
|
'''
|
||||||
|
arg."""
|
||||||
|
names = [c.name for c in Script(s).completions()]
|
||||||
|
assert 'join' in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_docstrings_type_str(Script):
|
||||||
|
s = """
|
||||||
|
def func(arg):
|
||||||
|
'''
|
||||||
|
:type arg: str
|
||||||
|
'''
|
||||||
|
arg."""
|
||||||
|
|
||||||
|
names = [c.name for c in Script(s).completions()]
|
||||||
|
assert 'join' in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_docstring_instance(Script):
|
||||||
|
# The types hint that it's a certain kind
|
||||||
|
s = dedent("""
|
||||||
|
class A:
|
||||||
|
def __init__(self,a):
|
||||||
|
'''
|
||||||
|
:type a: threading.Thread
|
||||||
'''
|
'''
|
||||||
|
|
||||||
c.""")
|
if a is not None:
|
||||||
|
a.start()
|
||||||
|
|
||||||
names = [c.name for c in jedi.Script(s).completions()]
|
self.a = a
|
||||||
assert 'a' in names
|
|
||||||
assert '__init__' in names
|
|
||||||
assert 'mro' not in names # Exists only for types.
|
def method_b(c):
|
||||||
|
'''
|
||||||
|
:type c: A
|
||||||
|
'''
|
||||||
|
|
||||||
|
c.""")
|
||||||
|
|
||||||
|
names = [c.name for c in Script(s).completions()]
|
||||||
|
assert 'a' in names
|
||||||
|
assert '__init__' in names
|
||||||
|
assert 'mro' not in names # Exists only for types.
|
||||||
|
|
||||||
|
|
||||||
|
def test_docstring_keyword(Script):
|
||||||
|
completions = Script('assert').completions()
|
||||||
|
assert 'assert' in completions[0].docstring()
|
||||||
|
|
||||||
def test_docstring_keyword(self):
|
|
||||||
completions = jedi.Script('assert').completions()
|
|
||||||
self.assertIn('assert', completions[0].docstring())
|
|
||||||
|
|
||||||
# ---- Numpy Style Tests ---
|
# ---- Numpy Style Tests ---
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_parameters():
|
def test_numpydoc_parameters():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -150,7 +160,8 @@ def test_numpydoc_parameters():
|
|||||||
assert 'isupper' in names
|
assert 'isupper' in names
|
||||||
assert 'capitalize' in names
|
assert 'capitalize' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_parameters_set_of_values():
|
def test_numpydoc_parameters_set_of_values():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -166,7 +177,8 @@ def test_numpydoc_parameters_set_of_values():
|
|||||||
assert 'capitalize' in names
|
assert 'capitalize' in names
|
||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_parameters_alternative_types():
|
def test_numpydoc_parameters_alternative_types():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -183,7 +195,8 @@ def test_numpydoc_parameters_alternative_types():
|
|||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
assert 'append' in names
|
assert 'append' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_returns():
|
def test_numpydoc_returns():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -204,7 +217,8 @@ def test_numpydoc_returns():
|
|||||||
assert 'capitalize' in names
|
assert 'capitalize' in names
|
||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_returns_set_of_values():
|
def test_numpydoc_returns_set_of_values():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -224,7 +238,8 @@ def test_numpydoc_returns_set_of_values():
|
|||||||
assert 'capitalize' in names
|
assert 'capitalize' in names
|
||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_returns_alternative_types():
|
def test_numpydoc_returns_alternative_types():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -244,8 +259,9 @@ def test_numpydoc_returns_alternative_types():
|
|||||||
assert 'capitalize' not in names
|
assert 'capitalize' not in names
|
||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
assert 'append' in names
|
assert 'append' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_returns_list_of():
|
def test_numpydoc_returns_list_of():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -265,7 +281,8 @@ def test_numpydoc_returns_list_of():
|
|||||||
assert 'isupper' not in names
|
assert 'isupper' not in names
|
||||||
assert 'capitalize' not in names
|
assert 'capitalize' not in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_returns_obj():
|
def test_numpydoc_returns_obj():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -285,7 +302,8 @@ def test_numpydoc_returns_obj():
|
|||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
assert 'seed' in names
|
assert 'seed' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_yields():
|
def test_numpydoc_yields():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
@@ -302,30 +320,28 @@ def test_numpydoc_yields():
|
|||||||
z = foobar():
|
z = foobar():
|
||||||
z.''')
|
z.''')
|
||||||
names = [c.name for c in jedi.Script(s).completions()]
|
names = [c.name for c in jedi.Script(s).completions()]
|
||||||
print('names',names)
|
|
||||||
assert 'isupper' in names
|
assert 'isupper' in names
|
||||||
assert 'capitalize' in names
|
assert 'capitalize' in names
|
||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||||
reason='numpydoc or numpy module is unavailable')
|
reason='numpydoc or numpy module is unavailable')
|
||||||
def test_numpy_returns():
|
def test_numpy_returns():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
import numpy
|
|
||||||
x = numpy.asarray([])
|
x = numpy.asarray([])
|
||||||
x.d''')
|
x.d''')
|
||||||
names = [c.name for c in jedi.Script(s).completions()]
|
names = [c.name for c in jedi.Script(s).completions()]
|
||||||
print(names)
|
print(names)
|
||||||
assert 'diagonal' in names
|
assert 'diagonal' in names
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
|
||||||
|
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||||
reason='numpydoc or numpy module is unavailable')
|
reason='numpydoc or numpy module is unavailable')
|
||||||
def test_numpy_comp_returns():
|
def test_numpy_comp_returns():
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
import numpy
|
|
||||||
x = numpy.array([])
|
x = numpy.array([])
|
||||||
x.d''')
|
x.d''')
|
||||||
names = [c.name for c in jedi.Script(s).completions()]
|
names = [c.name for c in jedi.Script(s).completions()]
|
||||||
print(names)
|
print(names)
|
||||||
assert 'diagonal' in names
|
assert 'diagonal' in names
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user