|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 | from __future__ import with_statement
|
4 | 4 |
|
5 |
| -from pytest import skip |
6 |
| - |
7 | 5 | import pep257
|
| 6 | +import mock |
8 | 7 |
|
9 | 8 |
|
10 |
| -__all__ = [] |
| 9 | +__all__ = () |
11 | 10 |
|
12 | 11 |
|
13 |
| -@skip |
14 | 12 | def test_pep257_conformance():
|
15 | 13 | errors = list(pep257.check(['pep257.py', 'test_pep257.py']))
|
16 | 14 | print(errors)
|
17 |
| - assert len(errors) == 0 |
18 |
| - |
19 |
| - |
20 |
| -def test_get_summary_line_info(): |
21 |
| - s1 = '''"""First line summary."""''' |
22 |
| - s2 = '''"""First line summary. |
23 |
| - With subsequent line. |
24 |
| - """''' |
25 |
| - s3 = '''""""""''' |
26 |
| - s4 = '''""" |
27 |
| - Second line summary. |
28 |
| - """''' |
29 |
| - s5 = '''""" |
30 |
| - Second line summary. |
31 |
| - With subsquent line. |
32 |
| - """''' |
33 |
| - assert pep257.get_summary_line_info(s1) == ('First line summary.', 0) |
34 |
| - assert pep257.get_summary_line_info(s2) == ('First line summary.', 0) |
35 |
| - assert pep257.get_summary_line_info(s3) == ('', 0) |
36 |
| - assert pep257.get_summary_line_info(s4) == ('Second line summary.', 1) |
37 |
| - assert pep257.get_summary_line_info(s5) == ('Second line summary.', 1) |
38 |
| - |
39 |
| - |
40 |
| -def test_parse_docstring(): |
41 |
| - s1 = '''def foo(): # o hai comment |
42 |
| - """docstring""" |
43 |
| - 2 + 2''' |
44 |
| - assert pep257.parse_docstring(s1) == '"""docstring"""' |
45 |
| - |
46 |
| - s2 = '''def foo(): # o hai comment |
47 |
| - 2 + 2''' |
48 |
| - assert pep257.parse_docstring(s2) is None |
49 |
| - |
50 |
| - assert pep257.parse_docstring("def foo():pass") is None |
51 |
| - # TODO |
52 |
| - # assert pep257.parse_docstring("def bar():'doc';pass") == "'doc'" |
53 |
| - |
54 |
| - |
55 |
| -def test_abs_pos(): |
56 |
| - assert pep257.abs_pos((1, 0), 'foo') == 0 |
57 |
| - assert pep257.abs_pos((1, 2), 'foo') == 2 |
58 |
| - assert pep257.abs_pos((2, 0), 'foo\nbar') == 4 |
59 |
| - |
60 |
| - |
61 |
| -def test_rel_pos(): |
62 |
| - assert pep257.rel_pos(0, 'foo') == (1, 0) |
63 |
| - assert pep257.rel_pos(2, 'foo') == (1, 2) |
64 |
| - assert pep257.rel_pos(4, 'foo\nbar') == (2, 0) |
65 |
| - assert pep257.rel_pos(6, 'foo\nbar') == (2, 2) |
66 |
| - |
67 |
| - |
68 |
| -def test_parse_functions(): |
69 |
| - parse = pep257.parse_functions |
70 |
| - assert parse('') == [] |
71 |
| - # TODO assert pf('def foo():pass') == ['def foo():pass'] |
72 |
| - assert parse('def foo():\n pass\n') == ['def foo():\n pass\n'] |
73 |
| - assert parse('def foo():\n pass') == ['def foo():\n pass'] |
74 |
| - f1 = '''def foo():\n pass\ndef bar():\n pass''' |
75 |
| - assert parse(f1) == ['def foo():\n pass\n', |
76 |
| - 'def bar():\n pass'] |
77 |
| - f2 = '''def foo():\n pass\noh, hai\ndef bar():\n pass''' |
78 |
| - assert parse(f2) == ['def foo():\n pass\n', |
79 |
| - 'def bar():\n pass'] |
80 |
| - |
81 |
| - |
82 |
| -def test_parse_methods(): |
83 |
| - parse = pep257.parse_methods |
84 |
| - assert parse('') == [] |
85 |
| - m1 = '''class Foo: |
86 |
| - def m1(): |
87 |
| - pass |
88 |
| - def m2(): |
89 |
| - pass''' |
90 |
| - assert parse(m1) == ['def m1():\n pass\n ', |
91 |
| - 'def m2():\n pass'] |
92 |
| - m2 = '''class Foo: |
93 |
| - def m1(): |
94 |
| - pass |
95 |
| - attribute |
96 |
| - def m2(): |
97 |
| - pass''' |
98 |
| - assert parse(m2) == ['def m1():\n pass\n ', |
99 |
| - 'def m2():\n pass'] |
100 |
| - |
101 |
| - |
102 |
| -def test_check_triple_double_quotes(): |
103 |
| - check = pep257.check_triple_double_quotes |
104 |
| - assert check("'''Not using triple douple quotes'''", None, None) |
105 |
| - assert not check('"""Using triple double quotes"""', None, None) |
106 |
| - assert not check('r"""Using raw triple double quotes"""', None, None) |
107 |
| - assert not check('u"""Using unicode triple double quotes"""', None, None) |
108 |
| - |
109 |
| - |
110 |
| -def test_check_backslashes(): |
111 |
| - check = pep257.check_backslashes |
112 |
| - assert check('"""backslash\\here""""', None, None) |
113 |
| - assert not check('r"""backslash\\here""""', None, None) |
114 |
| - |
115 |
| - |
116 |
| -def test_check_unicode_docstring(): |
117 |
| - check = pep257.check_unicode_docstring |
118 |
| - assert not check('"""No Unicode here."""', None, None) |
119 |
| - assert not check('u"""Здесь Юникод: øπΩ≈ç√∫˜µ≤"""', None, None) |
120 |
| - assert check('"""Здесь Юникод: øπΩ≈ç√∫˜µ≤"""', None, None) |
121 |
| - |
122 |
| - |
123 |
| -def test_check_ends_with_period(): |
124 |
| - check = pep257.check_ends_with_period |
125 |
| - s1 = '"""Should end with a period"""' |
126 |
| - s2 = '"""Should end with a period."""' |
127 |
| - s3 = '''""" |
128 |
| - Should end with a period |
129 |
| - """''' |
130 |
| - s4 = '''""" |
131 |
| - Should end with a period. |
132 |
| - """''' |
133 |
| - assert check(s1, None, None) |
134 |
| - assert not check(s2, None, None) |
135 |
| - assert check(s3, None, None) |
136 |
| - assert not check(s4, None, None) |
137 |
| - |
138 |
| - |
139 |
| -def test_check_blank_before_after_class(): |
140 |
| - check = pep257.check_blank_before_after_class |
141 |
| - c1 = '''class Perfect(object): |
142 |
| -
|
143 |
| - """This should work perfectly.""" |
144 |
| -
|
145 |
| - pass''' |
146 |
| - assert not check('"""This should work perfectly."""', c1, False) |
147 |
| - |
148 |
| - c2 = '''class BadTop(object): |
149 |
| - """This should fail due to a lack of whitespace above.""" |
150 |
| -
|
151 |
| - pass''' |
152 |
| - assert check('"""This should fail due to a lack of whitespace above."""', |
153 |
| - c2, False) |
154 |
| - c3 = '''class BadBottom(object): |
155 |
| -
|
156 |
| - """This should fail due to a lack of whitespace below.""" |
157 |
| - pass''' |
158 |
| - assert check('"""This should fail due to a lack of whitespace below."""', |
159 |
| - c3, False) |
160 |
| - c4 = '''class GoodWithNoFollowingWhiteSpace(object): |
161 |
| -
|
162 |
| - """This should work."""''' |
163 |
| - assert not check('"""This should work."""', |
164 |
| - c4, False) |
165 |
| - c5 = '''class GoodWithFollowingWhiteSpace(object): |
166 |
| -
|
167 |
| - """This should work.""" |
168 |
| -
|
169 |
| -
|
170 |
| -''' |
171 |
| - assert not check('"""This should work."""', c5, False) |
172 |
| - |
173 |
| - c6 = '''class Perfect(object): |
174 |
| -
|
175 |
| - """This should work perfectly.""" |
176 |
| -
|
177 |
| - def foo(self): |
178 |
| - """This should work perfectly.""" |
179 |
| - pass |
180 |
| -
|
181 |
| - ''' |
182 |
| - assert not check('"""This should work perfectly."""', c6, False) |
183 |
| - |
184 |
| - |
185 |
| -def test_check_blank_after_summary(): |
186 |
| - check = pep257.check_blank_after_summary |
187 |
| - s1 = '''"""Blank line missing after one-line summary. |
188 |
| - .................... |
189 |
| - """''' |
190 |
| - s2 = '''"""Blank line missing after one-line summary. |
191 |
| -
|
192 |
| - """''' |
193 |
| - s3 = '''""" |
194 |
| - Blank line missing after one-line summary. |
195 |
| - .................... |
196 |
| - """''' |
197 |
| - s4 = '''""" |
198 |
| - Blank line missing after one-line summary. |
199 |
| -
|
200 |
| - """''' |
201 |
| - assert check(s1, None, None) |
202 |
| - assert not check(s2, None, None) |
203 |
| - assert check(s3, None, None) |
204 |
| - assert not check(s4, None, None) |
205 |
| - |
206 |
| - |
207 |
| -def test_check_indent(): |
208 |
| - check = pep257.check_indent |
209 |
| - context = '''def foo(): |
210 |
| - """Docstring. |
211 |
| -
|
212 |
| - Properly indented. |
213 |
| -
|
214 |
| - """ |
215 |
| - pass''' |
216 |
| - assert not check('"""%s"""' % context.split('"""')[1], context, None) |
217 |
| - context = '''def foo(): |
218 |
| - """Docstring. |
| 15 | + assert errors == [] |
219 | 16 |
|
220 |
| -Not Properly indented. |
221 | 17 |
|
| 18 | +def test_ignore_list(): |
| 19 | + function_to_check = """def function_with_bad_docstring(foo): |
| 20 | + \"\"\" does spacing without a period in the end |
| 21 | + no blank line after one-liner is bad. Also this - \"\"\" |
| 22 | + return foo |
222 | 23 | """
|
223 |
| - pass''' |
224 |
| - assert check('"""%s"""' % context.split('"""')[1], context, None) |
| 24 | + expected_error_codes = set(('D100', 'D400', 'D401', 'D205', 'D209')) |
| 25 | + mock_open = mock.mock_open(read_data=function_to_check) |
| 26 | + with mock.patch('pep257.open', mock_open, create=True): |
| 27 | + errors = tuple(pep257.check(['filepath'])) |
| 28 | + error_codes = set(error.code for error in errors) |
| 29 | + assert error_codes == expected_error_codes |
| 30 | + |
| 31 | + errors = tuple(pep257.check(['filepath'], ignore=['D100', 'D202'])) |
| 32 | + error_codes = set(error.code for error in errors) |
| 33 | + assert error_codes == expected_error_codes - set(('D100', 'D202')) |
0 commit comments