mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 21:34:32 +08:00
Rework the test suite to make clearer where errors originate.
This commit is contained in:
@@ -7,6 +7,7 @@ if True:
|
|||||||
#: W101:0
|
#: W101:0
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
#: E122+1
|
||||||
change_2_log = \
|
change_2_log = \
|
||||||
"""Change 2 by slamb@testclient on 2006/04/13 21:46:23
|
"""Change 2 by slamb@testclient on 2006/04/13 21:46:23
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,35 @@ To easily verify if our normalizer raises the right error codes, just use the
|
|||||||
tests of pydocstyle.
|
tests of pydocstyle.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import difflib
|
||||||
import re
|
import re
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
from functools import total_ordering
|
||||||
|
|
||||||
import parso
|
import parso
|
||||||
|
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
|
class WantedIssue(object):
|
||||||
|
def __init__(self, code, line, column):
|
||||||
|
self.code = code
|
||||||
|
self._line = line
|
||||||
|
self._column = column
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.code == other.code and self.start_pos == other.start_pos
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
return self.start_pos < other.start_pos or self.code < other.code
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(str(self.code) + str(self._line) + str(self._column))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def start_pos(self):
|
||||||
|
return self._line, self._column
|
||||||
|
|
||||||
|
|
||||||
def collect_errors(code):
|
def collect_errors(code):
|
||||||
for line_nr, line in enumerate(code.splitlines(), 1):
|
for line_nr, line in enumerate(code.splitlines(), 1):
|
||||||
match = re.match(r'(\s*)#: (.*)$', line)
|
match = re.match(r'(\s*)#: (.*)$', line)
|
||||||
@@ -21,25 +44,26 @@ def collect_errors(code):
|
|||||||
code, _, add_line = code.partition('+')
|
code, _, add_line = code.partition('+')
|
||||||
l = line_nr + 1 + int(add_line or 0)
|
l = line_nr + 1 + int(add_line or 0)
|
||||||
|
|
||||||
yield "%s@(%s,%s)" % (code[1:], l, column)
|
yield WantedIssue(code[1:], l, column)
|
||||||
|
|
||||||
|
|
||||||
def test_normalizer_issue(normalizer_issue_file):
|
def test_normalizer_issue(normalizer_issue_file):
|
||||||
|
def sort(issues):
|
||||||
|
issues = sorted(issues, key=lambda i: (i.start_pos, i.code))
|
||||||
|
return ["(%s, %s): %s" % (i.start_pos[0], i.start_pos[1], i.code)
|
||||||
|
for i in issues]
|
||||||
|
|
||||||
with open(normalizer_issue_file.path) as f:
|
with open(normalizer_issue_file.path) as f:
|
||||||
code = f.read()
|
code = f.read()
|
||||||
|
|
||||||
desired = list(collect_errors(code))
|
desired = sort(collect_errors(code))
|
||||||
|
|
||||||
module = parso.parse(code)
|
module = parso.parse(code)
|
||||||
issues = module._get_normalizer_issues()
|
issues = module._get_normalizer_issues()
|
||||||
|
actual = sort(issues)
|
||||||
|
|
||||||
i = set("%s@(%s,%s)" % (i.code, i.start_pos[0], i.start_pos[1]) for i in issues)
|
diff = '\n'.join(difflib.ndiff(desired, actual))
|
||||||
d = set(desired)
|
# To make the pytest -v diff a bit prettier, stop pytest to rewrite assert
|
||||||
assert i == d, dedent("""
|
# statements by executing the comparison earlier.
|
||||||
Test %r failed (%s of %s passed).
|
_bool = desired == actual
|
||||||
not raised = %s
|
assert _bool, '\n' + diff
|
||||||
unexpected = %s
|
|
||||||
""") % (
|
|
||||||
normalizer_issue_file.name, len(i & d), len(d),
|
|
||||||
sorted(d - i), sorted(i - d)
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user