Cleanup the docstring tests

This commit is contained in:
Dave Halter
2017-12-29 19:47:28 +01:00
parent bf73fcbed4
commit c7266d65c1

View File

@@ -15,82 +15,89 @@ 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()
self.assertEqual(defs[0].docstring(), 'func()\n\nDocstring of `func`.') assert defs[0].docstring() == 'func()\n\nDocstring of `func`.'
def test_class_doc(self):
defs = jedi.Script(""" def test_class_doc(Script):
defs = Script("""
class TestClass(): class TestClass():
'''Docstring of `TestClass`.''' '''Docstring of `TestClass`.'''
TestClass""").goto_definitions() TestClass""").goto_definitions()
self.assertEqual(defs[0].docstring(), 'Docstring of `TestClass`.') assert defs[0].docstring() == 'Docstring of `TestClass`.'
def test_instance_doc(self):
defs = jedi.Script(""" def test_instance_doc(Script):
defs = Script("""
class TestClass(): class TestClass():
'''Docstring of `TestClass`.''' '''Docstring of `TestClass`.'''
tc = TestClass() tc = TestClass()
tc""").goto_definitions() tc""").goto_definitions()
self.assertEqual(defs[0].docstring(), 'Docstring of `TestClass`.') assert defs[0].docstring() == 'Docstring of `TestClass`.'
@unittest.skip('need evaluator class for that') @unittest.skip('need evaluator class for that')
def test_attribute_docstring(self): def test_attribute_docstring(Script):
defs = jedi.Script(""" defs = Script("""
x = None x = None
'''Docstring of `x`.''' '''Docstring of `x`.'''
x""").goto_definitions() x""").goto_definitions()
self.assertEqual(defs[0].docstring(), 'Docstring of `x`.') assert defs[0].docstring() == 'Docstring of `x`.'
@unittest.skip('need evaluator class for that') @unittest.skip('need evaluator class for that')
def test_multiple_docstrings(self): def test_multiple_docstrings(Script):
defs = jedi.Script(""" defs = Script("""
def func(): def func():
'''Original docstring.''' '''Original docstring.'''
x = func x = func
'''Docstring of `x`.''' '''Docstring of `x`.'''
x""").goto_definitions() x""").goto_definitions()
docs = [d.docstring() for d in defs] docs = [d.docstring() for d in defs]
self.assertEqual(docs, ['Original docstring.', 'Docstring of `x`.']) assert docs == ['Original docstring.', 'Docstring of `x`.']
def test_completion(self):
assert jedi.Script(''' def test_completion(Script):
assert Script('''
class DocstringCompletion(): class DocstringCompletion():
#? [] #? []
""" asdfas """''').completions() """ asdfas """''').completions()
def test_docstrings_type_dotted_import(self):
def test_docstrings_type_dotted_import(Script):
s = """ s = """
def func(arg): def func(arg):
''' '''
:type arg: random.Random :type arg: random.Random
''' '''
arg.""" arg."""
names = [c.name for c in jedi.Script(s).completions()] names = [c.name for c in Script(s).completions()]
assert 'seed' in names assert 'seed' in names
def test_docstrings_param_type(self):
def test_docstrings_param_type(Script):
s = """ s = """
def func(arg): def func(arg):
''' '''
:param str arg: some description :param str arg: some description
''' '''
arg.""" arg."""
names = [c.name for c in jedi.Script(s).completions()] names = [c.name for c in Script(s).completions()]
assert 'join' in names assert 'join' in names
def test_docstrings_type_str(self):
def test_docstrings_type_str(Script):
s = """ s = """
def func(arg): def func(arg):
''' '''
@@ -98,10 +105,11 @@ class TestDocstring(unittest.TestCase):
''' '''
arg.""" arg."""
names = [c.name for c in jedi.Script(s).completions()] names = [c.name for c in Script(s).completions()]
assert 'join' in names assert 'join' in names
def test_docstring_instance(self):
def test_docstring_instance(Script):
# The types hint that it's a certain kind # The types hint that it's a certain kind
s = dedent(""" s = dedent("""
class A: class A:
@@ -123,14 +131,16 @@ class TestDocstring(unittest.TestCase):
c.""") c.""")
names = [c.name for c in jedi.Script(s).completions()] names = [c.name for c in Script(s).completions()]
assert 'a' in names assert 'a' in names
assert '__init__' in names assert '__init__' in names
assert 'mro' not in names # Exists only for types. assert 'mro' not in names # Exists only for types.
def test_docstring_keyword(self):
completions = jedi.Script('assert').completions() def test_docstring_keyword(Script):
self.assertIn('assert', completions[0].docstring()) completions = Script('assert').completions()
assert 'assert' in completions[0].docstring()
# ---- Numpy Style Tests --- # ---- Numpy Style Tests ---
@@ -150,6 +160,7 @@ 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():
@@ -166,6 +177,7 @@ 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():
@@ -183,6 +195,7 @@ 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():
@@ -204,6 +217,7 @@ 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():
@@ -224,6 +238,7 @@ 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():
@@ -245,6 +260,7 @@ def test_numpydoc_returns_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_list_of(): def test_numpydoc_returns_list_of():
@@ -265,6 +281,7 @@ 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():
@@ -285,6 +302,7 @@ 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():
@@ -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