harness.py 1004 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import ast
  2. import textwrap
  3. import unittest
  4. from pyflakes import checker
  5. __all__ = ['TestCase', 'skip', 'skipIf']
  6. skip = unittest.skip
  7. skipIf = unittest.skipIf
  8. class TestCase(unittest.TestCase):
  9. withDoctest = False
  10. def flakes(self, input, *expectedOutputs, **kw):
  11. tree = ast.parse(textwrap.dedent(input))
  12. file_tokens = checker.make_tokens(textwrap.dedent(input))
  13. if kw.get('is_segment'):
  14. tree = tree.body[0]
  15. kw.pop('is_segment')
  16. w = checker.Checker(
  17. tree, file_tokens=file_tokens, withDoctest=self.withDoctest, **kw
  18. )
  19. outputs = [type(o) for o in w.messages]
  20. expectedOutputs = list(expectedOutputs)
  21. outputs.sort(key=lambda t: t.__name__)
  22. expectedOutputs.sort(key=lambda t: t.__name__)
  23. self.assertEqual(outputs, expectedOutputs, '''\
  24. for input:
  25. {}
  26. expected outputs:
  27. {!r}
  28. but got:
  29. {}'''.format(input, expectedOutputs, '\n'.join([str(o) for o in w.messages])))
  30. return w