harness.py 891 B

12345678910111213141516171819202122232425262728293031323334
  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. if kw.get('is_segment'):
  13. tree = tree.body[0]
  14. kw.pop('is_segment')
  15. w = checker.Checker(tree, withDoctest=self.withDoctest, **kw)
  16. outputs = [type(o) for o in w.messages]
  17. expectedOutputs = list(expectedOutputs)
  18. outputs.sort(key=lambda t: t.__name__)
  19. expectedOutputs.sort(key=lambda t: t.__name__)
  20. self.assertEqual(outputs, expectedOutputs, '''\
  21. for input:
  22. {}
  23. expected outputs:
  24. {!r}
  25. but got:
  26. {}'''.format(input, expectedOutputs, '\n'.join([str(o) for o in w.messages])))
  27. return w