fix_asserts.py 984 B

12345678910111213141516171819202122232425262728293031323334
  1. """Fixer that replaces deprecated unittest method names."""
  2. # Author: Ezio Melotti
  3. from ..fixer_base import BaseFix
  4. from ..fixer_util import Name
  5. NAMES = dict(
  6. assert_="assertTrue",
  7. assertEquals="assertEqual",
  8. assertNotEquals="assertNotEqual",
  9. assertAlmostEquals="assertAlmostEqual",
  10. assertNotAlmostEquals="assertNotAlmostEqual",
  11. assertRegexpMatches="assertRegex",
  12. assertRaisesRegexp="assertRaisesRegex",
  13. failUnlessEqual="assertEqual",
  14. failIfEqual="assertNotEqual",
  15. failUnlessAlmostEqual="assertAlmostEqual",
  16. failIfAlmostEqual="assertNotAlmostEqual",
  17. failUnless="assertTrue",
  18. failUnlessRaises="assertRaises",
  19. failIf="assertFalse",
  20. )
  21. class FixAsserts(BaseFix):
  22. PATTERN = """
  23. power< any+ trailer< '.' meth=(%s)> any* >
  24. """ % '|'.join(map(repr, NAMES))
  25. def transform(self, node, results):
  26. name = results["meth"][0]
  27. name.replace(Name(NAMES[str(name)], prefix=name.prefix))