Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.2 KiB

  1. # coding: utf-8
  2. """Tests for the elpy.black module"""
  3. import unittest
  4. import os
  5. from elpy import blackutil
  6. from elpy.rpc import Fault
  7. from elpy.tests.support import BackendTestCase
  8. @unittest.skipIf(blackutil.BLACK_NOT_SUPPORTED,
  9. 'black not supported for current python version')
  10. class BLACKTestCase(BackendTestCase):
  11. def setUp(self):
  12. if blackutil.BLACK_NOT_SUPPORTED:
  13. raise unittest.SkipTest
  14. def test_fix_code_should_throw_error_for_invalid_code(self):
  15. src = 'x = '
  16. self.assertRaises(Fault, blackutil.fix_code, src, os.getcwd())
  17. def test_fix_code_should_throw_error_without_black_installed(self):
  18. black = blackutil.black
  19. blackutil.black = None
  20. src = 'x= 123\n', 'x = 123\n'
  21. with self.assertRaises(Fault):
  22. blackutil.fix_code(src, os.getcwd())
  23. blackutil.black = black
  24. def test_fix_code(self):
  25. testdata = [
  26. ('x= 123\n', 'x = 123\n'),
  27. ('x=1; \ny=2 \n', 'x = 1\ny = 2\n'),
  28. ]
  29. for src, expected in testdata:
  30. self._assert_format(src, expected)
  31. def test_perfect_code(self):
  32. testdata = [
  33. ('x = 123\n', 'x = 123\n'),
  34. ('x = 1\ny = 2\n', 'x = 1\ny = 2\n'),
  35. ]
  36. for src, expected in testdata:
  37. self._assert_format(src, expected)
  38. def _assert_format(self, src, expected):
  39. new_block = blackutil.fix_code(src, os.getcwd())
  40. self.assertEqual(new_block, expected)
  41. def test_should_read_options_from_pyproject_toml(self):
  42. with open('pyproject.toml', 'w') as f:
  43. f.write('[tool.black]\nline-length = 10')
  44. self.addCleanup(os.remove, 'pyproject.toml')
  45. testdata = [('x= 123\n', 'x = 123\n'),
  46. ('x=1; \ny=2 \n', 'x = 1\ny = 2\n'),
  47. ('x, y, z, a, b, c = 123, 124, 125, 126, 127, 128',
  48. '(\n x,\n y,\n z,\n a,\n b,\n c,\n)'
  49. ' = (\n 123,\n 124,\n 125,'
  50. '\n 126,\n 127,\n 128,\n)\n')]
  51. for src, expected in testdata:
  52. self._assert_format(src, expected)