Skip to content

Commit 4376083

Browse files
committed
PyCQA#129 - Added UT
1 parent bb7c976 commit 4376083

File tree

5 files changed

+189
-2
lines changed

5 files changed

+189
-2
lines changed

src/pydocstyle/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def _check_section(cls, docstring, definition, context):
517517

518518
suffix = context.line.strip().lstrip(context.section_name)
519519
if suffix != '':
520-
yield violations.D406(capitalized_section, suffix)
520+
yield violations.D406(capitalized_section, context.line.strip())
521521

522522
if context.previous_line.strip() != '':
523523
yield violations.D411(capitalized_section)

src/pydocstyle/violations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def to_rst(cls):
212212
D405 = D4xx.create_error('D405', 'Section name should be properly capitalized',
213213
'{0!r}, not {1!r}')
214214
D406 = D4xx.create_error('D406', 'Section name should end with a newline',
215-
'{0!r}, found {1!r}')
215+
'{0!r}, not {1!r}')
216216
D407 = D4xx.create_error('D407', 'Missing dashed underline after section',
217217
'{0!r}')
218218
D408 = D4xx.create_error('D408', 'Section underline should be in the line '

src/tests/test_cases/sections.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
"""A valid module docstring."""
2+
3+
from .expected import Expectation
4+
5+
expectation = Expectation()
6+
expect = expectation.expect
7+
8+
9+
_D213 = 'D213: Multi-line docstring summary should start at the second line'
10+
11+
12+
@expect(_D213)
13+
@expect("D405: Section name should be properly capitalized "
14+
"('Returns', not 'returns')")
15+
def not_capitalized():
16+
"""Valid headline.
17+
18+
returns
19+
-------
20+
21+
"""
22+
23+
24+
@expect(_D213)
25+
@expect("D406: Section name should end with a newline "
26+
"('Returns', not 'Returns:')")
27+
def superfluous_suffix():
28+
"""Valid headline.
29+
30+
Returns:
31+
-------
32+
33+
"""
34+
35+
36+
@expect(_D213)
37+
@expect("D407: Missing dashed underline after section ('Returns')")
38+
def no_underline():
39+
"""Valid headline.
40+
41+
Returns
42+
43+
"""
44+
45+
46+
@expect(_D213)
47+
@expect("D408: Section underline should be in the line following the "
48+
"section\'s name ('Returns')")
49+
def blank_line_before_underline():
50+
"""Valid headline.
51+
52+
Returns
53+
54+
-------
55+
56+
"""
57+
58+
59+
@expect(_D213)
60+
@expect("D409: Section underline should match the length of its name "
61+
"(len('Returns') == 7, got 2 dashes)")
62+
def bad_underline_length():
63+
"""Valid headline.
64+
65+
Returns
66+
--
67+
68+
"""
69+
70+
71+
@expect(_D213)
72+
@expect("D410: Missing blank line after section ('Returns')")
73+
def no_blank_line_after_section():
74+
"""Valid headline.
75+
76+
Returns
77+
-------
78+
A whole lot of values.
79+
"""
80+
81+
@expect(_D213)
82+
@expect("D411: Missing blank line before section ('Returns')")
83+
def no_blank_line_before_section():
84+
"""Valid headline.
85+
86+
The function's description.
87+
Returns
88+
-------
89+
90+
"""
91+
92+
93+
@expect(_D213)
94+
@expect("D214: Section is over-indented ('Returns')")
95+
def section_overindented():
96+
"""Valid headline.
97+
98+
Returns
99+
-------
100+
101+
"""
102+
103+
104+
@expect(_D213)
105+
@expect("D215: Section underline is over-indented (in section 'Returns')")
106+
def section_underline_overindented():
107+
"""Valid headline.
108+
109+
Returns
110+
-------
111+
112+
"""
113+
114+
115+
@expect(_D213)
116+
def ignore_non_actual_section():
117+
"""Valid headline.
118+
119+
This is the function's description, which will also specify what it
120+
returns
121+
122+
"""
123+
124+
125+
@expect(_D213)
126+
@expect("D401: First line should be in imperative mood "
127+
"('Return', not 'Returns')")
128+
@expect("D400: First line should end with a period (not 's')")
129+
@expect("D205: 1 blank line required between summary line and description "
130+
"(found 0)")
131+
def section_name_in_first_line():
132+
"""Returns
133+
-------
134+
135+
"""
136+
137+
138+
@expect(_D213)
139+
@expect("D405: Section name should be properly capitalized "
140+
"('Short Summary', not 'Short summary')")
141+
@expect("D409: Section underline should match the length of its name "
142+
"(len('Returns') == 7, got 6 dashes)")
143+
@expect("D410: Missing blank line after section ('Returns')")
144+
@expect("D411: Missing blank line before section ('Raises')")
145+
@expect("D406: Section name should end with a newline "
146+
"('Raises', not 'Raises:')")
147+
@expect("D407: Missing dashed underline after section ('Raises')")
148+
@expect("D410: Missing blank line after section ('Raises')")
149+
def multiple_sections():
150+
"""Valid headline.
151+
152+
Short summary
153+
-------------
154+
155+
This is the function's description, which will also specify what it
156+
returns.
157+
158+
Returns
159+
------
160+
Many many wonderful things.
161+
Raises:
162+
My attention.
163+
164+
"""

src/tests/test_definitions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def test_token_stream():
278278
'all_import',
279279
'all_import_as',
280280
'superfluous_quotes',
281+
'sections',
281282
])
282283
def test_pep257(test_case):
283284
"""Run domain-specific tests from test.py file."""

src/tests/test_integration.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,28 @@ def foo():
501501
assert 'D213' not in out
502502

503503

504+
def test_numpy_convention(env):
505+
"""Test that the 'numpy' convention options has the correct errors."""
506+
with env.open('example.py', 'wt') as example:
507+
example.write(textwrap.dedent('''
508+
class Foo(object):
509+
"""Docstring for this class.
510+
511+
returns
512+
------
513+
"""
514+
'''))
515+
516+
env.write_config(convention="numpy")
517+
out, err, code = env.invoke()
518+
assert code == 1
519+
assert 'D213' not in out
520+
assert 'D215' in out
521+
assert 'D405' in out
522+
assert 'D409' in out
523+
assert 'D410' in out
524+
525+
504526
def test_config_file_inheritance(env):
505527
"""Test configuration files inheritance.
506528

0 commit comments

Comments
 (0)