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, List
3 from unittest.mock import patch
8 from tests.util import (
17 SIMPLE_CASES: List[str] = [
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",
58 EXPERIMENTAL_STRING_PROCESSING_CASES: List[str] = [
62 "long_strings__edge_case",
63 "long_strings__regression",
67 PY310_CASES: List[str] = [
68 "pattern_matching_simple",
69 "pattern_matching_complex",
70 "pattern_matching_extras",
71 "pattern_matching_style",
72 "pattern_matching_generic",
73 "parenthesized_context_managers",
76 PREVIEW_CASES: List[str] = []
78 SOURCES: List[str] = [
79 "src/black/__init__.py",
80 "src/black/__main__.py",
81 "src/black/brackets.py",
83 "src/black/comments.py",
84 "src/black/concurrency.py",
88 "src/black/linegen.py",
92 "src/black/numerics.py",
93 "src/black/output.py",
94 "src/black/parsing.py",
95 "src/black/report.py",
97 "src/black/strings.py",
99 "src/blackd/__init__.py",
100 "src/black_primer/cli.py",
101 "src/black_primer/lib.py",
102 "src/blib2to3/pygram.py",
103 "src/blib2to3/pytree.py",
104 "src/blib2to3/pgen2/conv.py",
105 "src/blib2to3/pgen2/driver.py",
106 "src/blib2to3/pgen2/grammar.py",
107 "src/blib2to3/pgen2/literals.py",
108 "src/blib2to3/pgen2/parse.py",
109 "src/blib2to3/pgen2/pgen.py",
110 "src/blib2to3/pgen2/tokenize.py",
111 "src/blib2to3/pgen2/token.py",
113 "tests/test_black.py",
114 "tests/test_blackd.py",
115 "tests/test_format.py",
116 "tests/test_primer.py",
123 @pytest.fixture(autouse=True)
124 def patch_dump_to_file(request: Any) -> Iterator[None]:
125 with patch("black.dump_to_file", dump_to_stderr):
129 def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
130 source, expected = read_data(filename, data=data)
131 assert_format(source, expected, mode, fast=False)
134 @pytest.mark.parametrize("filename", SIMPLE_CASES)
135 def test_simple_format(filename: str) -> None:
136 check_file(filename, DEFAULT_MODE)
139 @pytest.mark.parametrize("filename", EXPERIMENTAL_STRING_PROCESSING_CASES)
140 def test_experimental_format(filename: str) -> None:
141 check_file(filename, black.Mode(experimental_string_processing=True))
144 @pytest.mark.parametrize("filename", PREVIEW_CASES)
145 def test_preview_format(filename: str) -> None:
146 check_file(filename, black.Mode(preview=True))
149 @pytest.mark.parametrize("filename", SOURCES)
150 def test_source_is_formatted(filename: str) -> None:
151 path = THIS_DIR.parent / filename
152 check_file(str(path), DEFAULT_MODE, data=False)
160 def test_empty() -> None:
161 source = expected = ""
162 assert_format(source, expected)
165 def test_pep_572() -> None:
166 source, expected = read_data("pep_572")
167 assert_format(source, expected, minimum_version=(3, 8))
170 def test_pep_572_remove_parens() -> None:
171 source, expected = read_data("pep_572_remove_parens")
172 assert_format(source, expected, minimum_version=(3, 8))
175 def test_pep_572_do_not_remove_parens() -> None:
176 source, expected = read_data("pep_572_do_not_remove_parens")
177 # the AST safety checks will fail, but that's expected, just make sure no
178 # parentheses are touched
179 assert_format(source, expected, fast=True)
182 @pytest.mark.parametrize("major, minor", [(3, 9), (3, 10)])
183 def test_pep_572_newer_syntax(major: int, minor: int) -> None:
184 source, expected = read_data(f"pep_572_py{major}{minor}")
185 assert_format(source, expected, minimum_version=(major, minor))
188 def test_pep_570() -> None:
189 source, expected = read_data("pep_570")
190 assert_format(source, expected, minimum_version=(3, 8))
193 @pytest.mark.parametrize("filename", PY310_CASES)
194 def test_python_310(filename: str) -> None:
195 source, expected = read_data(filename)
196 mode = black.Mode(target_versions={black.TargetVersion.PY310})
197 assert_format(source, expected, mode, minimum_version=(3, 10))
200 def test_patma_invalid() -> None:
201 source, expected = read_data("pattern_matching_invalid")
202 mode = black.Mode(target_versions={black.TargetVersion.PY310})
203 with pytest.raises(black.parsing.InvalidInput) as exc_info:
204 assert_format(source, expected, mode, minimum_version=(3, 10))
206 exc_info.match("Cannot parse: 10:11")
209 def test_patma_hint() -> None:
210 source, expected = read_data("pattern_matching_simple")
211 mode = black.Mode(target_versions={black.TargetVersion.PY39})
212 with pytest.raises(black.parsing.InvalidInput) as exc_info:
213 assert_format(source, expected, mode, minimum_version=(3, 10))
215 exc_info.match(black.parsing.PY310_HINT)
218 def test_python_2_hint() -> None:
219 with pytest.raises(black.parsing.InvalidInput) as exc_info:
220 assert_format("print 'daylily'", "print 'daylily'")
221 exc_info.match(black.parsing.PY2_HINT)
224 def test_docstring_no_string_normalization() -> None:
225 """Like test_docstring but with string normalization off."""
226 source, expected = read_data("docstring_no_string_normalization")
227 mode = replace(DEFAULT_MODE, string_normalization=False)
228 assert_format(source, expected, mode)
231 def test_long_strings_flag_disabled() -> None:
232 """Tests for turning off the string processing logic."""
233 source, expected = read_data("long_strings_flag_disabled")
234 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
235 assert_format(source, expected, mode)
238 def test_numeric_literals() -> None:
239 source, expected = read_data("numeric_literals")
240 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
241 assert_format(source, expected, mode)
244 def test_numeric_literals_ignoring_underscores() -> None:
245 source, expected = read_data("numeric_literals_skip_underscores")
246 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
247 assert_format(source, expected, mode)
250 def test_stub() -> None:
251 mode = replace(DEFAULT_MODE, is_pyi=True)
252 source, expected = read_data("stub.pyi")
253 assert_format(source, expected, mode)
256 def test_python38() -> None:
257 source, expected = read_data("python38")
258 assert_format(source, expected, minimum_version=(3, 8))
261 def test_python39() -> None:
262 source, expected = read_data("python39")
263 assert_format(source, expected, minimum_version=(3, 9))