2014-01-18 12:56:23 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
test_napoleon_docstring
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Tests for :mod:`sphinx.ext.napoleon.docstring` module.
|
|
|
|
|
|
|
|
|
2015-01-03 14:41:14 -06:00
|
|
|
:copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
|
2014-01-18 12:56:23 -06:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import textwrap
|
|
|
|
from unittest import TestCase
|
|
|
|
|
2014-08-10 02:31:28 -05:00
|
|
|
from sphinx.ext.napoleon import Config
|
|
|
|
from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
|
|
|
|
from util import mock
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-01-18 12:56:23 -06:00
|
|
|
|
|
|
|
class BaseDocstringTest(TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleDocstringTest(BaseDocstringTest):
|
|
|
|
docstrings = [(
|
|
|
|
"""Single line summary""",
|
|
|
|
"""Single line summary"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Extended description
|
|
|
|
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Extended description
|
|
|
|
"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
arg1(str):Extended
|
|
|
|
description of arg1
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: **arg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of arg1"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
arg1(str):Extended
|
|
|
|
description of arg1
|
|
|
|
arg2 ( int ) : Extended
|
|
|
|
description of arg2
|
|
|
|
|
|
|
|
Keyword Args:
|
|
|
|
kwarg1(str):Extended
|
|
|
|
description of kwarg1
|
|
|
|
kwarg2 ( int ) : Extended
|
|
|
|
description of kwarg2""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: * **arg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of arg1
|
|
|
|
* **arg2** (*int*) --
|
|
|
|
Extended
|
|
|
|
description of arg2
|
|
|
|
|
|
|
|
:Keyword Arguments: * **kwarg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of kwarg1
|
|
|
|
* **kwarg2** (*int*) --
|
|
|
|
Extended
|
|
|
|
description of kwarg2"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
arg1(str):Extended
|
|
|
|
description of arg1
|
|
|
|
arg2 ( int ) : Extended
|
|
|
|
description of arg2
|
|
|
|
|
|
|
|
Keyword Arguments:
|
|
|
|
kwarg1(str):Extended
|
|
|
|
description of kwarg1
|
|
|
|
kwarg2 ( int ) : Extended
|
|
|
|
description of kwarg2""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: * **arg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of arg1
|
|
|
|
* **arg2** (*int*) --
|
|
|
|
Extended
|
|
|
|
description of arg2
|
|
|
|
|
|
|
|
:Keyword Arguments: * **kwarg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of kwarg1
|
|
|
|
* **kwarg2** (*int*) --
|
|
|
|
Extended
|
|
|
|
description of kwarg2"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Return:
|
|
|
|
str:Extended
|
|
|
|
description of return value
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:returns: *str* --
|
|
|
|
Extended
|
|
|
|
description of return value"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str:Extended
|
|
|
|
description of return value
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:returns: *str* --
|
|
|
|
Extended
|
|
|
|
description of return value"""
|
2014-03-21 18:21:18 -05:00
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Extended
|
|
|
|
description of return value
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:returns: Extended
|
|
|
|
description of return value"""
|
2015-01-12 16:08:51 -06:00
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Args:
|
|
|
|
arg1(str):Extended
|
|
|
|
description of arg1
|
|
|
|
*args: Variable length argument list.
|
|
|
|
**kwargs: Arbitrary keyword arguments.
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: * **arg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of arg1
|
|
|
|
* **\\*args** --
|
|
|
|
Variable length argument list.
|
|
|
|
* **\\*\\*kwargs** --
|
|
|
|
Arbitrary keyword arguments."""
|
2014-01-18 12:56:23 -06:00
|
|
|
)]
|
|
|
|
|
|
|
|
def test_docstrings(self):
|
2014-03-09 00:56:56 -06:00
|
|
|
config = Config(napoleon_use_param=False, napoleon_use_rtype=False)
|
2014-01-18 12:56:23 -06:00
|
|
|
for docstring, expected in self.docstrings:
|
2014-03-09 00:56:56 -06:00
|
|
|
actual = str(GoogleDocstring(textwrap.dedent(docstring), config))
|
2014-01-18 12:56:23 -06:00
|
|
|
expected = textwrap.dedent(expected)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
def test_parameters_with_class_reference(self):
|
|
|
|
docstring = """\
|
|
|
|
Construct a new XBlock.
|
|
|
|
|
|
|
|
This class should only be used by runtimes.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
runtime (:class:`Runtime`): Use it to access the environment.
|
|
|
|
It is available in XBlock code as ``self.runtime``.
|
|
|
|
|
|
|
|
field_data (:class:`FieldData`): Interface used by the XBlock
|
|
|
|
fields to access their data from wherever it is persisted.
|
|
|
|
|
|
|
|
scope_ids (:class:`ScopeIds`): Identifiers needed to resolve scopes.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
actual = str(GoogleDocstring(docstring))
|
|
|
|
expected = """\
|
|
|
|
Construct a new XBlock.
|
|
|
|
|
|
|
|
This class should only be used by runtimes.
|
|
|
|
|
|
|
|
:param runtime: Use it to access the environment.
|
|
|
|
It is available in XBlock code as ``self.runtime``.
|
|
|
|
|
|
|
|
:type runtime: :class:`Runtime`
|
|
|
|
:param field_data: Interface used by the XBlock
|
|
|
|
fields to access their data from wherever it is persisted.
|
|
|
|
|
|
|
|
:type field_data: :class:`FieldData`
|
|
|
|
:param scope_ids: Identifiers needed to resolve scopes.
|
|
|
|
|
|
|
|
:type scope_ids: :class:`ScopeIds`
|
|
|
|
"""
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2014-06-13 15:06:39 -05:00
|
|
|
def test_attributes_with_class_reference(self):
|
|
|
|
docstring = """\
|
|
|
|
Attributes:
|
|
|
|
in_attr(:class:`numpy.ndarray`): super-dooper attribute
|
|
|
|
"""
|
|
|
|
|
|
|
|
actual = str(GoogleDocstring(docstring))
|
|
|
|
expected = """\
|
|
|
|
.. attribute:: in_attr
|
|
|
|
|
|
|
|
:class:`numpy.ndarray`
|
|
|
|
|
|
|
|
super-dooper attribute
|
|
|
|
"""
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
docstring = """\
|
|
|
|
Attributes:
|
|
|
|
in_attr(numpy.ndarray): super-dooper attribute
|
|
|
|
"""
|
|
|
|
|
|
|
|
actual = str(GoogleDocstring(docstring))
|
|
|
|
expected = """\
|
|
|
|
.. attribute:: in_attr
|
|
|
|
|
|
|
|
*numpy.ndarray*
|
|
|
|
|
|
|
|
super-dooper attribute
|
|
|
|
"""
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2014-01-18 12:56:23 -06:00
|
|
|
|
|
|
|
class NumpyDocstringTest(BaseDocstringTest):
|
|
|
|
docstrings = [(
|
|
|
|
"""Single line summary""",
|
|
|
|
"""Single line summary"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Extended description
|
|
|
|
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Extended description
|
|
|
|
"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
arg1:str
|
|
|
|
Extended
|
|
|
|
description of arg1
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: **arg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of arg1"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
arg1:str
|
|
|
|
Extended
|
|
|
|
description of arg1
|
|
|
|
arg2 : int
|
|
|
|
Extended
|
|
|
|
description of arg2
|
|
|
|
|
|
|
|
Keyword Arguments
|
|
|
|
-----------------
|
|
|
|
kwarg1:str
|
|
|
|
Extended
|
|
|
|
description of kwarg1
|
|
|
|
kwarg2 : int
|
|
|
|
Extended
|
|
|
|
description of kwarg2
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: * **arg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of arg1
|
|
|
|
* **arg2** (*int*) --
|
|
|
|
Extended
|
|
|
|
description of arg2
|
|
|
|
|
|
|
|
:Keyword Arguments: * **kwarg1** (*str*) --
|
|
|
|
Extended
|
|
|
|
description of kwarg1
|
|
|
|
* **kwarg2** (*int*) --
|
|
|
|
Extended
|
|
|
|
description of kwarg2"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Return
|
|
|
|
------
|
|
|
|
str
|
|
|
|
Extended
|
|
|
|
description of return value
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:returns: *str* --
|
|
|
|
Extended
|
|
|
|
description of return value"""
|
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
str
|
|
|
|
Extended
|
|
|
|
description of return value
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:returns: *str* --
|
|
|
|
Extended
|
|
|
|
description of return value"""
|
2015-01-12 16:08:51 -06:00
|
|
|
), (
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
arg1:str
|
|
|
|
Extended description of arg1
|
|
|
|
*args:
|
|
|
|
Variable length argument list.
|
|
|
|
**kwargs:
|
|
|
|
Arbitrary keyword arguments.
|
|
|
|
""",
|
|
|
|
"""
|
|
|
|
Single line summary
|
|
|
|
|
|
|
|
:Parameters: * **arg1** (*str*) --
|
|
|
|
Extended description of arg1
|
|
|
|
* ***args** --
|
|
|
|
Variable length argument list.
|
|
|
|
* ****kwargs** --
|
|
|
|
Arbitrary keyword arguments."""
|
2014-01-18 12:56:23 -06:00
|
|
|
)]
|
|
|
|
|
|
|
|
def test_docstrings(self):
|
2014-03-09 00:56:56 -06:00
|
|
|
config = Config(napoleon_use_param=False, napoleon_use_rtype=False)
|
2014-01-18 12:56:23 -06:00
|
|
|
for docstring, expected in self.docstrings:
|
2014-03-09 00:56:56 -06:00
|
|
|
actual = str(NumpyDocstring(textwrap.dedent(docstring), config))
|
2014-01-18 12:56:23 -06:00
|
|
|
expected = textwrap.dedent(expected)
|
|
|
|
self.assertEqual(expected, actual)
|
2014-03-09 00:56:56 -06:00
|
|
|
|
|
|
|
def test_parameters_with_class_reference(self):
|
2014-03-21 18:21:18 -05:00
|
|
|
docstring = """\
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
param1 : :class:`MyClass <name.space.MyClass>` instance
|
2014-03-09 00:56:56 -06:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
"""
|
2014-03-09 00:56:56 -06:00
|
|
|
|
|
|
|
config = Config(napoleon_use_param=False)
|
2014-03-21 18:21:18 -05:00
|
|
|
actual = str(NumpyDocstring(docstring, config))
|
|
|
|
expected = """\
|
2014-03-11 02:50:49 -05:00
|
|
|
:Parameters: **param1** (:class:`MyClass <name.space.MyClass>` instance)
|
2014-03-21 18:21:18 -05:00
|
|
|
"""
|
2014-03-09 00:56:56 -06:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
config = Config(napoleon_use_param=True)
|
2014-03-21 18:21:18 -05:00
|
|
|
actual = str(NumpyDocstring(docstring, config))
|
|
|
|
expected = """\
|
2015-01-09 11:41:09 -06:00
|
|
|
:param param1:
|
2014-03-21 18:21:18 -05:00
|
|
|
:type param1: :class:`MyClass <name.space.MyClass>` instance
|
|
|
|
"""
|
2014-03-09 00:56:56 -06:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_parameters_without_class_reference(self):
|
2014-03-21 18:21:18 -05:00
|
|
|
docstring = """\
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
param1 : MyClass instance
|
2014-03-09 00:56:56 -06:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
"""
|
2014-03-09 00:56:56 -06:00
|
|
|
|
|
|
|
config = Config(napoleon_use_param=False)
|
2014-03-21 18:21:18 -05:00
|
|
|
actual = str(NumpyDocstring(docstring, config))
|
|
|
|
expected = """\
|
|
|
|
:Parameters: **param1** (*MyClass instance*)
|
|
|
|
"""
|
2014-03-09 00:56:56 -06:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
config = Config(napoleon_use_param=True)
|
|
|
|
actual = str(NumpyDocstring(textwrap.dedent(docstring), config))
|
2014-03-21 18:21:18 -05:00
|
|
|
expected = """\
|
2015-01-09 11:41:09 -06:00
|
|
|
:param param1:
|
2014-03-21 18:21:18 -05:00
|
|
|
:type param1: MyClass instance
|
|
|
|
"""
|
2014-03-09 00:56:56 -06:00
|
|
|
self.assertEqual(expected, actual)
|
2014-03-09 21:07:21 -05:00
|
|
|
|
|
|
|
def test_see_also_refs(self):
|
2014-03-21 18:21:18 -05:00
|
|
|
docstring = """\
|
|
|
|
numpy.multivariate_normal(mean, cov, shape=None, spam=None)
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
some, other, funcs
|
|
|
|
otherfunc : relationship
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
"""
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
actual = str(NumpyDocstring(docstring))
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
expected = """\
|
2014-03-09 21:07:21 -05:00
|
|
|
numpy.multivariate_normal(mean, cov, shape=None, spam=None)
|
|
|
|
|
|
|
|
.. seealso::
|
2014-03-21 18:21:18 -05:00
|
|
|
|
|
|
|
:obj:`some`, :obj:`other`, :obj:`funcs`
|
|
|
|
\n\
|
|
|
|
:obj:`otherfunc`
|
2014-03-09 21:07:21 -05:00
|
|
|
relationship
|
|
|
|
"""
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
docstring = """\
|
|
|
|
numpy.multivariate_normal(mean, cov, shape=None, spam=None)
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
some, other, funcs
|
|
|
|
otherfunc : relationship
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
"""
|
2014-03-09 21:07:21 -05:00
|
|
|
|
|
|
|
config = Config()
|
2014-08-10 02:31:28 -05:00
|
|
|
app = mock.Mock()
|
2014-03-21 18:21:18 -05:00
|
|
|
actual = str(NumpyDocstring(docstring, config, app, "method"))
|
2014-03-09 21:07:21 -05:00
|
|
|
|
2014-03-21 18:21:18 -05:00
|
|
|
expected = """\
|
2014-03-09 21:07:21 -05:00
|
|
|
numpy.multivariate_normal(mean, cov, shape=None, spam=None)
|
|
|
|
|
|
|
|
.. seealso::
|
2014-03-21 18:21:18 -05:00
|
|
|
|
|
|
|
:meth:`some`, :meth:`other`, :meth:`funcs`
|
|
|
|
\n\
|
|
|
|
:meth:`otherfunc`
|
2014-03-09 21:07:21 -05:00
|
|
|
relationship
|
|
|
|
"""
|
|
|
|
self.assertEqual(expected, actual)
|