Skip to content

Commit 39f2f22

Browse files
committed
TST: Add test of validation warnings.
1 parent a1a7074 commit 39f2f22

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

numpydoc/tests/test_numpydoc.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# -*- encoding:utf-8 -*-
2+
import pytest
3+
from io import StringIO
24
from copy import deepcopy
35
from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature
46
from numpydoc.xref import DEFAULT_LINKS
57
from sphinx.ext.autodoc import ALL
8+
from sphinx.util import logging
69

710

811
class MockConfig():
@@ -33,6 +36,10 @@ class MockApp():
3336

3437
def __init__(self):
3538
self.builder.app = self
39+
# Attrs required for logging
40+
self.verbosity = 2
41+
self._warncount = 0
42+
self.warningiserror = False
3643

3744

3845
def test_mangle_docstrings():
@@ -92,6 +99,58 @@ def test_clean_text_signature():
9299
assert _clean_text_signature('func($self, *args)') == 'func(*args)'
93100

94101

102+
@pytest.fixture
103+
def f():
104+
def _function_without_seealso_and_examples():
105+
"""
106+
A function whose docstring has no examples or see also section.
107+
108+
Expect SA01 and EX01 errors if validation enabled.
109+
"""
110+
pass
111+
return _function_without_seealso_and_examples
112+
113+
114+
@pytest.mark.parametrize(
115+
(
116+
'numpydoc_validate',
117+
'numpydoc_validation_checks',
118+
'expected_warn',
119+
'non_warnings',
120+
),
121+
(
122+
# Validation configured off - expect no warnings
123+
(False, set(['SA01', 'EX01']), [], []),
124+
# Validation on with expected warnings
125+
(True, set(['SA01', 'EX01']), ('SA01', 'EX01'), []),
126+
# Validation on with only one activated check
127+
(True, set(['SA01']), ('SA01',), ('EX01',)),
128+
),
129+
)
130+
def test_mangle_docstring_validation_warnings(
131+
f,
132+
numpydoc_validate,
133+
numpydoc_validation_checks,
134+
expected_warn,
135+
non_warnings,
136+
):
137+
app = MockApp()
138+
# Set up config for test
139+
app.config.numpydoc_validate = numpydoc_validate
140+
app.config.numpydoc_validation_checks = numpydoc_validation_checks
141+
# Set up logging
142+
status, warning = StringIO(), StringIO()
143+
logging.setup(app, status, warning)
144+
# Run mangle docstrings with the above configuration
145+
mangle_docstrings(app, 'function', 'f', f, None, f.__doc__.split('\n'))
146+
# Assert that all (and only) expected warnings are logged
147+
warnings = warning.getvalue()
148+
for w in expected_warn:
149+
assert w in warnings
150+
for w in non_warnings:
151+
assert w not in warnings
152+
153+
95154
if __name__ == "__main__":
96155
import pytest
97156
pytest.main()

0 commit comments

Comments
 (0)