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.
1 from dataclasses import replace
2 from typing import Any, Iterator
3 from unittest.mock import patch
8 from tests.util import (
18 "beginning_backslash",
20 "class_blank_parentheses",
21 "class_methods_new_line",
29 "comments_non_breaking_space",
30 "comment_after_escaped_newline",
32 "composition_no_trailing_comma",
49 "function_trailing_comma",
54 "tricky_unicode_symbols",
59 "numeric_literals_py2",
61 "python2_unicode_literals",
64 EXPERIMENTAL_STRING_PROCESSING_CASES = [
68 "long_strings__edge_case",
69 "long_strings__regression",
74 "pattern_matching_simple",
75 "pattern_matching_complex",
76 "pattern_matching_extras",
77 "pattern_matching_style",
78 "parenthesized_context_managers",
82 "src/black/__init__.py",
83 "src/black/__main__.py",
84 "src/black/brackets.py",
86 "src/black/comments.py",
87 "src/black/concurrency.py",
91 "src/black/linegen.py",
95 "src/black/numerics.py",
96 "src/black/output.py",
97 "src/black/parsing.py",
98 "src/black/report.py",
100 "src/black/strings.py",
101 "src/black/trans.py",
102 "src/blackd/__init__.py",
103 "src/black_primer/cli.py",
104 "src/black_primer/lib.py",
105 "src/blib2to3/pygram.py",
106 "src/blib2to3/pytree.py",
107 "src/blib2to3/pgen2/conv.py",
108 "src/blib2to3/pgen2/driver.py",
109 "src/blib2to3/pgen2/grammar.py",
110 "src/blib2to3/pgen2/literals.py",
111 "src/blib2to3/pgen2/parse.py",
112 "src/blib2to3/pgen2/pgen.py",
113 "src/blib2to3/pgen2/tokenize.py",
114 "src/blib2to3/pgen2/token.py",
116 "tests/test_black.py",
117 "tests/test_blackd.py",
118 "tests/test_format.py",
119 "tests/test_primer.py",
126 @pytest.fixture(autouse=True)
127 def patch_dump_to_file(request: Any) -> Iterator[None]:
128 with patch("black.dump_to_file", dump_to_stderr):
132 def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
133 source, expected = read_data(filename, data=data)
134 assert_format(source, expected, mode, fast=False)
137 @pytest.mark.parametrize("filename", SIMPLE_CASES_PY2)
139 def test_simple_format_py2(filename: str) -> None:
140 check_file(filename, DEFAULT_MODE)
143 @pytest.mark.parametrize("filename", SIMPLE_CASES)
144 def test_simple_format(filename: str) -> None:
145 check_file(filename, DEFAULT_MODE)
148 @pytest.mark.parametrize("filename", EXPERIMENTAL_STRING_PROCESSING_CASES)
149 def test_experimental_format(filename: str) -> None:
150 check_file(filename, black.Mode(experimental_string_processing=True))
153 @pytest.mark.parametrize("filename", SOURCES)
154 def test_source_is_formatted(filename: str) -> None:
155 path = THIS_DIR.parent / filename
156 check_file(str(path), DEFAULT_MODE, data=False)
164 def test_empty() -> None:
165 source = expected = ""
166 assert_format(source, expected)
169 def test_pep_572() -> None:
170 source, expected = read_data("pep_572")
171 assert_format(source, expected, minimum_version=(3, 8))
174 def test_pep_572_remove_parens() -> None:
175 source, expected = read_data("pep_572_remove_parens")
176 assert_format(source, expected, minimum_version=(3, 8))
179 def test_pep_572_do_not_remove_parens() -> None:
180 source, expected = read_data("pep_572_do_not_remove_parens")
181 # the AST safety checks will fail, but that's expected, just make sure no
182 # parentheses are touched
183 assert_format(source, expected, fast=True)
186 @pytest.mark.parametrize("major, minor", [(3, 9), (3, 10)])
187 def test_pep_572_newer_syntax(major: int, minor: int) -> None:
188 source, expected = read_data(f"pep_572_py{major}{minor}")
189 assert_format(source, expected, minimum_version=(major, minor))
192 def test_pep_570() -> None:
193 source, expected = read_data("pep_570")
194 assert_format(source, expected, minimum_version=(3, 8))
197 @pytest.mark.parametrize("filename", PY310_CASES)
198 def test_python_310(filename: str) -> None:
199 source, expected = read_data(filename)
200 mode = black.Mode(target_versions={black.TargetVersion.PY310})
201 assert_format(source, expected, mode, minimum_version=(3, 10))
204 def test_patma_invalid() -> None:
205 source, expected = read_data("pattern_matching_invalid")
206 mode = black.Mode(target_versions={black.TargetVersion.PY310})
207 with pytest.raises(black.parsing.InvalidInput) as exc_info:
208 assert_format(source, expected, mode, minimum_version=(3, 10))
210 exc_info.match("Cannot parse: 10:11")
213 def test_patma_hint() -> None:
214 source, expected = read_data("pattern_matching_simple")
215 mode = black.Mode(target_versions={black.TargetVersion.PY39})
216 with pytest.raises(black.parsing.InvalidInput) as exc_info:
217 assert_format(source, expected, mode, minimum_version=(3, 10))
219 exc_info.match(black.parsing.PY310_HINT)
222 def test_docstring_no_string_normalization() -> None:
223 """Like test_docstring but with string normalization off."""
224 source, expected = read_data("docstring_no_string_normalization")
225 mode = replace(DEFAULT_MODE, string_normalization=False)
226 assert_format(source, expected, mode)
229 def test_long_strings_flag_disabled() -> None:
230 """Tests for turning off the string processing logic."""
231 source, expected = read_data("long_strings_flag_disabled")
232 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
233 assert_format(source, expected, mode)
236 def test_numeric_literals() -> None:
237 source, expected = read_data("numeric_literals")
238 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
239 assert_format(source, expected, mode)
242 def test_numeric_literals_ignoring_underscores() -> None:
243 source, expected = read_data("numeric_literals_skip_underscores")
244 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
245 assert_format(source, expected, mode)
249 def test_python2_print_function() -> None:
250 source, expected = read_data("python2_print_function")
251 mode = replace(DEFAULT_MODE, target_versions={black.TargetVersion.PY27})
252 assert_format(source, expected, mode)
255 def test_stub() -> None:
256 mode = replace(DEFAULT_MODE, is_pyi=True)
257 source, expected = read_data("stub.pyi")
258 assert_format(source, expected, mode)
261 def test_python38() -> None:
262 source, expected = read_data("python38")
263 assert_format(source, expected, minimum_version=(3, 8))
266 def test_python39() -> None:
267 source, expected = read_data("python39")
268 assert_format(source, expected, minimum_version=(3, 9))