]>
Commit | Line | Data |
---|---|---|
1 | # coding: utf-8 | |
2 | """Tests for the elpy.black module""" | |
3 | ||
4 | import unittest | |
5 | import os | |
6 | ||
7 | from elpy import blackutil | |
8 | from elpy.rpc import Fault | |
9 | from elpy.tests.support import BackendTestCase | |
10 | ||
11 | ||
12 | @unittest.skipIf(blackutil.BLACK_NOT_SUPPORTED, | |
13 | 'black not supported for current python version') | |
14 | class BLACKTestCase(BackendTestCase): | |
15 | def setUp(self): | |
16 | if blackutil.BLACK_NOT_SUPPORTED: | |
17 | raise unittest.SkipTest | |
18 | ||
19 | def test_fix_code_should_throw_error_for_invalid_code(self): | |
20 | src = 'x = ' | |
21 | self.assertRaises(Fault, blackutil.fix_code, src, os.getcwd()) | |
22 | ||
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 | |
30 | ||
31 | def test_fix_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 | ||
39 | def test_perfect_code(self): | |
40 | testdata = [ | |
41 | ('x = 123\n', 'x = 123\n'), | |
42 | ('x = 1\ny = 2\n', 'x = 1\ny = 2\n'), | |
43 | ] | |
44 | for src, expected in testdata: | |
45 | self._assert_format(src, expected) | |
46 | ||
47 | def _assert_format(self, src, expected): | |
48 | new_block = blackutil.fix_code(src, os.getcwd()) | |
49 | self.assertEqual(new_block, expected) | |
50 | ||
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') | |
54 | ||
55 | self.addCleanup(os.remove, 'pyproject.toml') | |
56 | ||
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) |