]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpa/elpy-20230803.1455/elpy/tests/test_black.py
3809c7b325b8addd400fb724e44913998d641fd9
2 """Tests for the elpy.black module"""
7 from elpy
import blackutil
8 from elpy
.rpc
import Fault
9 from elpy
.tests
.support
import BackendTestCase
12 @unittest.skipIf(blackutil
.BLACK_NOT_SUPPORTED
,
13 'black not supported for current python version')
14 class BLACKTestCase(BackendTestCase
):
16 if blackutil
.BLACK_NOT_SUPPORTED
:
17 raise unittest
.SkipTest
19 def test_fix_code_should_throw_error_for_invalid_code(self
):
21 self
.assertRaises(Fault
, blackutil
.fix_code
, src
, os
.getcwd())
23 def test_fix_code_should_throw_error_without_black_installed(self
):
24 black
= blackutil
.black
25 blackutil
.black
= None
26 src
= 'x= 123\n', 'x = 123\n'
27 with self
.assertRaises(Fault
):
28 blackutil
.fix_code(src
, os
.getcwd())
29 blackutil
.black
= black
31 def test_fix_code(self
):
33 ('x= 123\n', 'x = 123\n'),
34 ('x=1; \ny=2 \n', 'x = 1\ny = 2\n'),
36 for src
, expected
in testdata
:
37 self
._assert
_format
(src
, expected
)
39 def test_perfect_code(self
):
41 ('x = 123\n', 'x = 123\n'),
42 ('x = 1\ny = 2\n', 'x = 1\ny = 2\n'),
44 for src
, expected
in testdata
:
45 self
._assert
_format
(src
, expected
)
47 def _assert_format(self
, src
, expected
):
48 new_block
= blackutil
.fix_code(src
, os
.getcwd())
49 self
.assertEqual(new_block
, expected
)
51 def test_should_read_options_from_pyproject_toml(self
):
52 with
open('pyproject.toml', 'w') as f
:
53 f
.write('[tool.black]\nline-length = 10')
55 self
.addCleanup(os
.remove
, 'pyproject.toml')
57 testdata
= [('x= 123\n', 'x = 123\n'),
58 ('x=1; \ny=2 \n', 'x = 1\ny = 2\n'),
59 ('x, y, z, a, b, c = 123, 124, 125, 126, 127, 128',
60 '(\n x,\n y,\n z,\n a,\n b,\n c,\n)'
61 ' = (\n 123,\n 124,\n 125,'
62 '\n 126,\n 127,\n 128,\n)\n')]
63 for src
, expected
in testdata
:
64 self
._assert
_format
(src
, expected
)