All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 from functools import partial
3 from pathlib import Path
4 from typing import List, Tuple
6 from unittest.mock import patch
8 from click import unstyle
13 ff = partial(black.format_file, line_length=ll, fast=True)
14 fs = partial(black.format_str, line_length=ll)
15 THIS_FILE = Path(__file__)
16 THIS_DIR = THIS_FILE.parent
19 def dump_to_stderr(*output: str) -> str:
20 return '\n' + '\n'.join(output) + '\n'
23 def read_data(name: str) -> Tuple[str, str]:
24 """read_data('test_name') -> 'input', 'output'"""
25 if not name.endswith('.py'):
27 _input: List[str] = []
28 _output: List[str] = []
29 with open(THIS_DIR / name, 'r', encoding='utf8') as test:
30 lines = test.readlines()
33 if line.rstrip() == '# output':
38 if _input and not _output:
39 # If there's no output marker, treat the entire file as already pre-formatted.
41 return ''.join(_input).strip() + '\n', ''.join(_output).strip() + '\n'
44 class BlackTestCase(unittest.TestCase):
47 def assertFormatEqual(self, expected: str, actual: str) -> None:
48 if actual != expected:
49 black.out('Expected tree:', fg='green')
51 exp_node = black.lib2to3_parse(expected)
52 bdv = black.DebugVisitor()
53 list(bdv.visit(exp_node))
54 except Exception as ve:
56 black.out('Actual tree:', fg='red')
58 exp_node = black.lib2to3_parse(actual)
59 bdv = black.DebugVisitor()
60 list(bdv.visit(exp_node))
61 except Exception as ve:
63 self.assertEqual(expected, actual)
65 @patch("black.dump_to_file", dump_to_stderr)
66 def test_self(self) -> None:
67 source, expected = read_data('test_black')
69 self.assertFormatEqual(expected, actual)
70 black.assert_equivalent(source, actual)
71 black.assert_stable(source, actual, line_length=ll)
72 with self.assertRaises(black.NothingChanged):
75 @patch("black.dump_to_file", dump_to_stderr)
76 def test_black(self) -> None:
77 source, expected = read_data('../black')
79 self.assertFormatEqual(expected, actual)
80 black.assert_equivalent(source, actual)
81 black.assert_stable(source, actual, line_length=ll)
82 with self.assertRaises(black.NothingChanged):
85 @patch("black.dump_to_file", dump_to_stderr)
86 def test_setup(self) -> None:
87 source, expected = read_data('../setup')
89 self.assertFormatEqual(expected, actual)
90 black.assert_equivalent(source, actual)
91 black.assert_stable(source, actual, line_length=ll)
92 with self.assertRaises(black.NothingChanged):
95 @patch("black.dump_to_file", dump_to_stderr)
96 def test_function(self) -> None:
97 source, expected = read_data('function')
99 self.assertFormatEqual(expected, actual)
100 black.assert_equivalent(source, actual)
101 black.assert_stable(source, actual, line_length=ll)
103 @patch("black.dump_to_file", dump_to_stderr)
104 def test_expression(self) -> None:
105 source, expected = read_data('expression')
107 self.assertFormatEqual(expected, actual)
108 black.assert_equivalent(source, actual)
109 black.assert_stable(source, actual, line_length=ll)
111 @patch("black.dump_to_file", dump_to_stderr)
112 def test_fstring(self) -> None:
113 source, expected = read_data('fstring')
115 self.assertFormatEqual(expected, actual)
116 black.assert_equivalent(source, actual)
117 black.assert_stable(source, actual, line_length=ll)
119 @patch("black.dump_to_file", dump_to_stderr)
120 def test_comments(self) -> None:
121 source, expected = read_data('comments')
123 self.assertFormatEqual(expected, actual)
124 black.assert_equivalent(source, actual)
125 black.assert_stable(source, actual, line_length=ll)
127 @patch("black.dump_to_file", dump_to_stderr)
128 def test_comments2(self) -> None:
129 source, expected = read_data('comments2')
131 self.assertFormatEqual(expected, actual)
132 black.assert_equivalent(source, actual)
133 black.assert_stable(source, actual, line_length=ll)
135 @patch("black.dump_to_file", dump_to_stderr)
136 def test_cantfit(self) -> None:
137 source, expected = read_data('cantfit')
139 self.assertFormatEqual(expected, actual)
140 black.assert_equivalent(source, actual)
141 black.assert_stable(source, actual, line_length=ll)
143 @patch("black.dump_to_file", dump_to_stderr)
144 def test_import_spacing(self) -> None:
145 source, expected = read_data('import_spacing')
147 self.assertFormatEqual(expected, actual)
148 black.assert_equivalent(source, actual)
149 black.assert_stable(source, actual, line_length=ll)
151 @patch("black.dump_to_file", dump_to_stderr)
152 def test_composition(self) -> None:
153 source, expected = read_data('composition')
155 self.assertFormatEqual(expected, actual)
156 black.assert_equivalent(source, actual)
157 black.assert_stable(source, actual, line_length=ll)
159 def test_report(self) -> None:
160 report = black.Report()
164 def out(msg: str, **kwargs):
165 out_lines.append(msg)
167 def err(msg: str, **kwargs):
168 err_lines.append(msg)
170 with patch("black.out", out), patch("black.err", err):
171 report.done(Path('f1'), changed=False)
172 self.assertEqual(len(out_lines), 1)
173 self.assertEqual(len(err_lines), 0)
174 self.assertEqual(out_lines[-1], 'f1 already well formatted, good job.')
175 self.assertEqual(unstyle(str(report)), '1 file left unchanged.')
176 self.assertEqual(report.return_code, 0)
177 report.done(Path('f2'), changed=True)
178 self.assertEqual(len(out_lines), 2)
179 self.assertEqual(len(err_lines), 0)
180 self.assertEqual(out_lines[-1], 'reformatted f2')
182 unstyle(str(report)), '1 file reformatted, 1 file left unchanged.'
184 self.assertEqual(report.return_code, 1)
185 report.failed(Path('e1'), 'boom')
186 self.assertEqual(len(out_lines), 2)
187 self.assertEqual(len(err_lines), 1)
188 self.assertEqual(err_lines[-1], 'error: cannot format e1: boom')
190 unstyle(str(report)),
191 '1 file reformatted, 1 file left unchanged, '
192 '1 file failed to reformat.',
194 self.assertEqual(report.return_code, 123)
195 report.done(Path('f3'), changed=True)
196 self.assertEqual(len(out_lines), 3)
197 self.assertEqual(len(err_lines), 1)
198 self.assertEqual(out_lines[-1], 'reformatted f3')
200 unstyle(str(report)),
201 '2 files reformatted, 1 file left unchanged, '
202 '1 file failed to reformat.',
204 self.assertEqual(report.return_code, 123)
205 report.failed(Path('e2'), 'boom')
206 self.assertEqual(len(out_lines), 3)
207 self.assertEqual(len(err_lines), 2)
208 self.assertEqual(err_lines[-1], 'error: cannot format e2: boom')
210 unstyle(str(report)),
211 '2 files reformatted, 1 file left unchanged, '
212 '2 files failed to reformat.',
214 self.assertEqual(report.return_code, 123)
215 report.done(Path('f4'), changed=False)
216 self.assertEqual(len(out_lines), 4)
217 self.assertEqual(len(err_lines), 2)
218 self.assertEqual(out_lines[-1], 'f4 already well formatted, good job.')
220 unstyle(str(report)),
221 '2 files reformatted, 2 files left unchanged, '
222 '2 files failed to reformat.',
224 self.assertEqual(report.return_code, 123)
226 def test_is_python36(self):
227 node = black.lib2to3_parse("def f(*, arg): ...\n")
228 self.assertFalse(black.is_python36(node))
229 node = black.lib2to3_parse("def f(*, arg,): ...\n")
230 self.assertTrue(black.is_python36(node))
231 node = black.lib2to3_parse("def f(*, arg): f'string'\n")
232 self.assertTrue(black.is_python36(node))
233 source, expected = read_data('function')
234 node = black.lib2to3_parse(source)
235 self.assertTrue(black.is_python36(node))
236 node = black.lib2to3_parse(expected)
237 self.assertTrue(black.is_python36(node))
238 source, expected = read_data('expression')
239 node = black.lib2to3_parse(source)
240 self.assertFalse(black.is_python36(node))
241 node = black.lib2to3_parse(expected)
242 self.assertFalse(black.is_python36(node))
245 if __name__ == '__main__':