Klimi's new dotfiles with stow.
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.

55 lines
1.8 KiB

4 years ago
  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(self):
  18. testdata = [
  19. ('x= 123\n', 'x = 123\n'),
  20. ('x=1; \ny=2 \n', 'x = 1\ny = 2\n'),
  21. ]
  22. for src, expected in testdata:
  23. self._assert_format(src, expected)
  24. def test_perfect_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 _assert_format(self, src, expected):
  32. new_block = blackutil.fix_code(src, os.getcwd())
  33. self.assertEqual(new_block, expected)
  34. def test_should_read_options_from_pyproject_toml(self):
  35. with open('pyproject.toml', 'w') as f:
  36. f.write('[tool.black]\nline-length = 10')
  37. self.addCleanup(os.remove, 'pyproject.toml')
  38. testdata = [('x= 123\n', 'x = 123\n'),
  39. ('x=1; \ny=2 \n', 'x = 1\ny = 2\n'),
  40. ('x, y, z, a, b, c = 123, 124, 125, 126, 127, 128',
  41. 'x, y, z, a, b, c = (\n 123,\n 124,\n 125,'
  42. '\n 126,\n 127,\n 128,\n)\n')]
  43. for src, expected in testdata:
  44. self._assert_format(src, expected)