add tests to make sure the warning is raised

This commit is contained in:
Keewis
2020-07-13 15:49:03 +02:00
parent b66f3e143e
commit 467533ffe8
2 changed files with 46 additions and 1 deletions

View File

@@ -883,7 +883,7 @@ class NumpyDocstring(GoogleDocstring):
filepath = inspect.getfile(self._obj) if self._obj is not None else None
name = self._name
if filepath is None and name is None:
if filepath is None and not name:
return None
return "{}: docstring of {}:".format(filepath, name)

View File

@@ -9,11 +9,15 @@
:license: BSD, see LICENSE for details.
"""
import re
from collections import namedtuple
from contextlib import contextmanager
from inspect import cleandoc
from textwrap import dedent
from unittest import TestCase, mock
import pytest
from sphinx.ext.napoleon import Config
from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
@@ -2003,3 +2007,44 @@ Do as you please
:kwtype gotham_is_yours: None
"""
self.assertEqual(expected, actual)
@contextmanager
def warns(warning, match):
match_re = re.compile(match)
try:
yield warning
finally:
raw_warnings = warning.getvalue()
warnings = [w for w in raw_warnings.split("\n") if w.strip()]
assert len(warnings) == 1 and all(match_re.match(w) for w in warnings)
class TestNumpyDocstring:
@pytest.mark.parametrize(
["spec", "pattern"],
(
pytest.param("*args, *kwargs", ".+: can only combine parameters of form", id="two args"),
pytest.param("**args, **kwargs", ".+: can only combine parameters of form", id="two kwargs"),
pytest.param(
"*args, **kwargs, other_parameter",
".+: cannot combine .+ and .+ with more parameters",
id="more parameters",
),
pytest.param("**kwargs, *args", r".+: wrong order of .+ and .+", id="swapped parameters"),
)
)
def test_invalid_combined_args_and_kwargs(self, spec, pattern, app, warning):
docstring = dedent(
"""\
Parameters
----------
{}
variable args list and arbitrary keyword arguments
"""
).format(spec)
config = Config()
with warns(warning, match=pattern):
str(NumpyDocstring(docstring, config, app, "method"))