This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
test_sphinx.py
81 lines (64 loc) · 2.49 KB
/
test_sphinx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Unit tests for Sphinx-style parameter documentation rules.
Use tox or pytest to run the test suite.
"""
from pydocstyle.checker import ConventionChecker
import textwrap
import pytest
SPHINX_ARGS_REGEX = ConventionChecker.SPHINX_ARGS_REGEX
def test_parameter_found():
"""The regex matches a line with a parameter definition."""
line = " :param x: Lorem ipsum dolor sit amet\n"
assert SPHINX_ARGS_REGEX.match(line) is not None
def test_parameter_name_extracted():
"""The first match group is the parameter name."""
line = " :param foo: Lorem ipsum dolor sit amet\n"
assert SPHINX_ARGS_REGEX.match(line).group(1) == "foo"
def test_finding_params():
"""Sphinx-style parameter names are found."""
docstring = """A description of a great function.
:param foo: Lorem ipsum dolor sit amet
:param bar: Ut enim ad minim veniam
"""
lines = docstring.splitlines(keepends=True)
assert ConventionChecker._find_sphinx_params(lines) == ['foo', 'bar']
def test_missing_params():
"""Missing parameters are reported."""
source = textwrap.dedent('''\
def thing(foo, bar, baz):
"""Do great things.
:param foo: Lorem ipsum dolor sit amet
:param baz: Ut enim ad minim veniam
"""
pass
''')
errors = ConventionChecker().check_source(source, '<test>')
for error in errors:
if error.code == "D417":
break
else:
pytest.fail('did not find D417 error')
assert error.parameters == ('bar', 'thing')
assert error.message == (
"D417: Missing argument descriptions in the docstring (argument(s) bar are"
" missing descriptions in 'thing' docstring)")
def test_missing_description():
"""A parameter is considered missing if it has no description."""
source = textwrap.dedent('''\
def thing(foo, bar, baz):
"""Do great things.
:param foo: Lorem ipsum dolor sit amet
:param bar:
:param baz: Ut enim ad minim veniam
"""
pass
''')
errors = ConventionChecker().check_source(source, '<test>')
for error in errors:
if error.code == "D417":
break
else:
pytest.fail('did not find D417 error')
assert error.parameters == ('bar', 'thing')
assert error.message == (
"D417: Missing argument descriptions in the docstring (argument(s) bar are"
" missing descriptions in 'thing' docstring)")