Skip to content

Commit 91d1433

Browse files
authored
Merge pull request #1146 from PyCQA/reusable-testsuite-methods
make testsuite helper for generating errors
2 parents fecdcf1 + 84839aa commit 91d1433

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

testsuite/support.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import annotations
3+
24
import os.path
35
import re
46
import sys
57

68
from pycodestyle import Checker, BaseReport, StandardReport, readlines
9+
from pycodestyle import StyleGuide
710

811
SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}):\s(.*)')
912
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
@@ -222,3 +225,13 @@ def run_tests(style):
222225
if options.testsuite:
223226
init_tests(style)
224227
return style.check_files()
228+
229+
230+
def errors_from_src(src: str) -> list[str]:
231+
guide = StyleGuide()
232+
reporter = guide.init_report(InMemoryReport)
233+
guide.input_file(
234+
filename='in-memory-test-file.py',
235+
lines=src.splitlines(True),
236+
)
237+
return reporter.in_memory_errors

testsuite/test_blank_lines.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,14 @@
66
import unittest
77

88
import pycodestyle
9-
from testsuite.support import InMemoryReport
9+
from testsuite.support import errors_from_src
1010

1111

1212
class BlankLinesTestCase(unittest.TestCase):
1313
"""
1414
Common code for running blank_lines tests.
1515
"""
1616

17-
def check(self, content):
18-
"""
19-
Run checks on `content` and return the the list of errors.
20-
"""
21-
sut = pycodestyle.StyleGuide()
22-
reporter = sut.init_report(InMemoryReport)
23-
sut.input_file(
24-
filename='in-memory-test-file.py',
25-
lines=content.splitlines(True),
26-
)
27-
return reporter.in_memory_errors
28-
2917
def assertNoErrors(self, actual):
3018
"""
3119
Check that the actual result from the checker has no errors.
@@ -43,7 +31,7 @@ def test_initial_no_blank(self):
4331
"""
4432
It will accept no blank lines at the start of the file.
4533
"""
46-
result = self.check("""def some_function():
34+
result = errors_from_src("""def some_function():
4735
pass
4836
""")
4937

@@ -54,7 +42,7 @@ def test_initial_lines_one_blank(self):
5442
It will accept 1 blank lines before the first line of actual
5543
code, even if in other places it asks for 2
5644
"""
57-
result = self.check("""
45+
result = errors_from_src("""
5846
def some_function():
5947
pass
6048
""")
@@ -66,7 +54,7 @@ def test_initial_lines_two_blanks(self):
6654
It will accept 2 blank lines before the first line of actual
6755
code, as normal.
6856
"""
69-
result = self.check("""
57+
result = errors_from_src("""
7058
7159
def some_function():
7260
pass
@@ -79,7 +67,7 @@ def test_method_less_blank_lines(self):
7967
It will trigger an error when less than 1 blank lin is found
8068
before method definitions.
8169
"""
82-
result = self.check("""# First comment line.
70+
result = errors_from_src("""# First comment line.
8371
class X:
8472
8573
def a():
@@ -96,7 +84,7 @@ def test_method_less_blank_lines_comment(self):
9684
It will trigger an error when less than 1 blank lin is found
9785
before method definition, ignoring comments.
9886
"""
99-
result = self.check("""# First comment line.
87+
result = errors_from_src("""# First comment line.
10088
class X:
10189
10290
def a():
@@ -114,7 +102,7 @@ def test_top_level_fewer_blank_lines(self):
114102
It will trigger an error when less 2 blank lines are found
115103
before top level definitions.
116104
"""
117-
result = self.check("""# First comment line.
105+
result = errors_from_src("""# First comment line.
118106
# Second line of comment.
119107
120108
def some_function():
@@ -148,7 +136,7 @@ def test_top_level_more_blank_lines(self):
148136
It will trigger an error when more 2 blank lines are found
149137
before top level definitions.
150138
"""
151-
result = self.check("""# First comment line.
139+
result = errors_from_src("""# First comment line.
152140
# Second line of comment.
153141
154142
@@ -179,7 +167,7 @@ def test_method_more_blank_lines(self):
179167
It will trigger an error when more than 1 blank line is found
180168
before method definition
181169
"""
182-
result = self.check("""# First comment line.
170+
result = errors_from_src("""# First comment line.
183171
184172
185173
class SomeCloseClass(object):
@@ -211,7 +199,7 @@ def test_initial_lines_more_blank(self):
211199
It will trigger an error for more than 2 blank lines before the
212200
first line of actual code.
213201
"""
214-
result = self.check("""
202+
result = errors_from_src("""
215203
216204
217205
def some_function():
@@ -224,7 +212,7 @@ def test_blank_line_between_decorator(self):
224212
It will trigger an error when the decorator is followed by a
225213
blank line.
226214
"""
227-
result = self.check("""# First line.
215+
result = errors_from_src("""# First line.
228216
229217
230218
@some_decorator
@@ -247,7 +235,7 @@ def test_blank_line_decorator(self):
247235
It will accept the decorators which are adjacent to the function
248236
and method definition.
249237
"""
250-
result = self.check("""# First line.
238+
result = errors_from_src("""# First line.
251239
252240
253241
@another_decorator
@@ -269,7 +257,7 @@ def test_top_level_fewer_follow_lines(self):
269257
It will trigger an error when less than 2 blank lines are
270258
found between a top level definitions and other top level code.
271259
"""
272-
result = self.check("""
260+
result = errors_from_src("""
273261
def a():
274262
print('Something')
275263
@@ -285,7 +273,7 @@ def test_top_level_fewer_follow_lines_comments(self):
285273
found between a top level definitions and other top level code,
286274
even if we have comments before
287275
"""
288-
result = self.check("""
276+
result = errors_from_src("""
289277
def a():
290278
print('Something')
291279
@@ -306,7 +294,7 @@ def test_top_level_good_follow_lines(self):
306294
It not trigger an error when 2 blank lines are
307295
found between a top level definitions and other top level code.
308296
"""
309-
result = self.check("""
297+
result = errors_from_src("""
310298
def a():
311299
print('Something')
312300
@@ -326,7 +314,7 @@ def test_method_fewer_follow_lines(self):
326314
It will trigger an error when less than 1 blank line is
327315
found between a method and previous definitions.
328316
"""
329-
result = self.check("""
317+
result = errors_from_src("""
330318
def a():
331319
x = 1
332320
def b():
@@ -342,7 +330,7 @@ def test_method_nested_fewer_follow_lines(self):
342330
found between a method and previous definitions, even when
343331
nested.
344332
"""
345-
result = self.check("""
333+
result = errors_from_src("""
346334
def a():
347335
x = 2
348336
@@ -361,7 +349,7 @@ def test_method_nested_less_class(self):
361349
between a method and previous definitions, even when used to
362350
define a class.
363351
"""
364-
result = self.check("""
352+
result = errors_from_src("""
365353
def a():
366354
x = 1
367355
class C:
@@ -377,7 +365,7 @@ def test_method_nested_ok(self):
377365
found between a method and previous definitions, even when
378366
nested.
379367
"""
380-
result = self.check("""
368+
result = errors_from_src("""
381369
def a():
382370
x = 2
383371
@@ -412,7 +400,7 @@ def test_initial_lines_one_blanks(self):
412400
It will accept less than 3 blank lines before the first line of
413401
actual code.
414402
"""
415-
result = self.check("""
403+
result = errors_from_src("""
416404
417405
418406
def some_function():
@@ -426,7 +414,7 @@ def test_initial_lines_tree_blanks(self):
426414
It will accept 3 blank lines before the first line of actual
427415
code, as normal.
428416
"""
429-
result = self.check("""
417+
result = errors_from_src("""
430418
431419
432420
def some_function():
@@ -440,7 +428,7 @@ def test_top_level_fewer_blank_lines(self):
440428
It will trigger an error when less 3 blank lines are found
441429
before top level definitions.
442430
"""
443-
result = self.check("""# First comment line.
431+
result = errors_from_src("""# First comment line.
444432
# Second line of comment.
445433
446434
@@ -479,7 +467,7 @@ def test_top_level_more_blank_lines(self):
479467
It will trigger an error when more 2 blank lines are found
480468
before top level definitions.
481469
"""
482-
result = self.check("""# First comment line.
470+
result = errors_from_src("""# First comment line.
483471
# Second line of comment.
484472
485473
@@ -513,7 +501,7 @@ def test_the_right_blanks(self):
513501
"""
514502
It will accept 3 blank for top level and 2 for nested.
515503
"""
516-
result = self.check("""
504+
result = errors_from_src("""
517505
518506
519507
def some_function():

0 commit comments

Comments
 (0)