test_normalizer_issues_files.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. To easily verify if our normalizer raises the right error codes, just use the
  3. tests of pydocstyle.
  4. """
  5. import difflib
  6. import re
  7. from functools import total_ordering
  8. from typing import Iterator, Tuple
  9. import parso
  10. from parso.utils import python_bytes_to_unicode
  11. @total_ordering
  12. class WantedIssue:
  13. def __init__(self, code: str, line: int, column: int) -> None:
  14. self.code = code
  15. self._line = line
  16. self._column = column
  17. def __eq__(self, other):
  18. return self.code == other.code and self.start_pos == other.start_pos
  19. def __lt__(self, other: 'WantedIssue') -> bool:
  20. return self.start_pos < other.start_pos or self.code < other.code
  21. def __hash__(self) -> int:
  22. return hash(str(self.code) + str(self._line) + str(self._column))
  23. @property
  24. def start_pos(self) -> Tuple[int, int]:
  25. return self._line, self._column
  26. def collect_errors(code: str) -> Iterator[WantedIssue]:
  27. for line_nr, line in enumerate(code.splitlines(), 1):
  28. match = re.match(r'(\s*)#: (.*)$', line)
  29. if match is not None:
  30. codes = match.group(2)
  31. for code in codes.split():
  32. code, _, add_indent = code.partition(':')
  33. column = int(add_indent or len(match.group(1)))
  34. code, _, add_line = code.partition('+')
  35. ln = line_nr + 1 + int(add_line or 0)
  36. yield WantedIssue(code[1:], ln, column)
  37. def test_normalizer_issue(normalizer_issue_case):
  38. def sort(issues):
  39. issues = sorted(issues, key=lambda i: (i.start_pos, i.code))
  40. return ["(%s, %s): %s" % (i.start_pos[0], i.start_pos[1], i.code)
  41. for i in issues]
  42. with open(normalizer_issue_case.path, 'rb') as f:
  43. code = python_bytes_to_unicode(f.read())
  44. desired = sort(collect_errors(code))
  45. grammar = parso.load_grammar(version=normalizer_issue_case.python_version)
  46. module = grammar.parse(code)
  47. issues = grammar._get_normalizer_issues(module)
  48. actual = sort(issues)
  49. diff = '\n'.join(difflib.ndiff(desired, actual))
  50. # To make the pytest -v diff a bit prettier, stop pytest to rewrite assert
  51. # statements by executing the comparison earlier.
  52. _bool = desired == actual
  53. assert _bool, '\n' + diff