|
1 | 1 | # -*- encoding:utf-8 -*-
|
| 2 | +import pytest |
| 3 | +from io import StringIO |
2 | 4 | from copy import deepcopy
|
3 | 5 | from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature
|
4 | 6 | from numpydoc.xref import DEFAULT_LINKS
|
5 | 7 | from sphinx.ext.autodoc import ALL
|
| 8 | +from sphinx.util import logging |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class MockConfig():
|
@@ -33,6 +36,10 @@ class MockApp():
|
33 | 36 |
|
34 | 37 | def __init__(self):
|
35 | 38 | self.builder.app = self
|
| 39 | + # Attrs required for logging |
| 40 | + self.verbosity = 2 |
| 41 | + self._warncount = 0 |
| 42 | + self.warningiserror = False |
36 | 43 |
|
37 | 44 |
|
38 | 45 | def test_mangle_docstrings():
|
@@ -92,6 +99,58 @@ def test_clean_text_signature():
|
92 | 99 | assert _clean_text_signature('func($self, *args)') == 'func(*args)'
|
93 | 100 |
|
94 | 101 |
|
| 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 | + |
95 | 154 | if __name__ == "__main__":
|
96 | 155 | import pytest
|
97 | 156 | pytest.main()
|
0 commit comments