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",
58 EXPERIMENTAL_STRING_PROCESSING_CASES = [
62 "long_strings__edge_case",
63 "long_strings__regression",
68 "pattern_matching_simple",
69 "pattern_matching_complex",
70 "pattern_matching_extras",
71 "pattern_matching_style",
72 "parenthesized_context_managers",
76 "src/black/__init__.py",
77 "src/black/__main__.py",
78 "src/black/brackets.py",
80 "src/black/comments.py",
81 "src/black/concurrency.py",
85 "src/black/linegen.py",
89 "src/black/numerics.py",
90 "src/black/output.py",
91 "src/black/parsing.py",
92 "src/black/report.py",
94 "src/black/strings.py",
96 "src/blackd/__init__.py",
97 "src/black_primer/cli.py",
98 "src/black_primer/lib.py",
99 "src/blib2to3/pygram.py",
100 "src/blib2to3/pytree.py",
101 "src/blib2to3/pgen2/conv.py",
102 "src/blib2to3/pgen2/driver.py",
103 "src/blib2to3/pgen2/grammar.py",
104 "src/blib2to3/pgen2/literals.py",
105 "src/blib2to3/pgen2/parse.py",
106 "src/blib2to3/pgen2/pgen.py",
107 "src/blib2to3/pgen2/tokenize.py",
108 "src/blib2to3/pgen2/token.py",
110 "tests/test_black.py",
111 "tests/test_blackd.py",
112 "tests/test_format.py",
113 "tests/test_primer.py",
120 @pytest.fixture(autouse=True)
121 def patch_dump_to_file(request: Any) -> Iterator[None]:
122 with patch("black.dump_to_file", dump_to_stderr):
126 def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
127 source, expected = read_data(filename, data=data)
128 assert_format(source, expected, mode, fast=False)
131 @pytest.mark.parametrize("filename", SIMPLE_CASES)
132 def test_simple_format(filename: str) -> None:
133 check_file(filename, DEFAULT_MODE)
136 @pytest.mark.parametrize("filename", EXPERIMENTAL_STRING_PROCESSING_CASES)
137 def test_experimental_format(filename: str) -> None:
138 check_file(filename, black.Mode(experimental_string_processing=True))
141 @pytest.mark.parametrize("filename", SOURCES)
142 def test_source_is_formatted(filename: str) -> None:
143 path = THIS_DIR.parent / filename
144 check_file(str(path), DEFAULT_MODE, data=False)
152 def test_empty() -> None:
153 source = expected = ""
154 assert_format(source, expected)
157 def test_pep_572() -> None:
158 source, expected = read_data("pep_572")
159 assert_format(source, expected, minimum_version=(3, 8))
162 def test_pep_572_remove_parens() -> None:
163 source, expected = read_data("pep_572_remove_parens")
164 assert_format(source, expected, minimum_version=(3, 8))
167 def test_pep_572_do_not_remove_parens() -> None:
168 source, expected = read_data("pep_572_do_not_remove_parens")
169 # the AST safety checks will fail, but that's expected, just make sure no
170 # parentheses are touched
171 assert_format(source, expected, fast=True)
174 @pytest.mark.parametrize("major, minor", [(3, 9), (3, 10)])
175 def test_pep_572_newer_syntax(major: int, minor: int) -> None:
176 source, expected = read_data(f"pep_572_py{major}{minor}")
177 assert_format(source, expected, minimum_version=(major, minor))
180 def test_pep_570() -> None:
181 source, expected = read_data("pep_570")
182 assert_format(source, expected, minimum_version=(3, 8))
185 @pytest.mark.parametrize("filename", PY310_CASES)
186 def test_python_310(filename: str) -> None:
187 source, expected = read_data(filename)
188 mode = black.Mode(target_versions={black.TargetVersion.PY310})
189 assert_format(source, expected, mode, minimum_version=(3, 10))
192 def test_patma_invalid() -> None:
193 source, expected = read_data("pattern_matching_invalid")
194 mode = black.Mode(target_versions={black.TargetVersion.PY310})
195 with pytest.raises(black.parsing.InvalidInput) as exc_info:
196 assert_format(source, expected, mode, minimum_version=(3, 10))
198 exc_info.match("Cannot parse: 10:11")
201 def test_patma_hint() -> None:
202 source, expected = read_data("pattern_matching_simple")
203 mode = black.Mode(target_versions={black.TargetVersion.PY39})
204 with pytest.raises(black.parsing.InvalidInput) as exc_info:
205 assert_format(source, expected, mode, minimum_version=(3, 10))
207 exc_info.match(black.parsing.PY310_HINT)
210 def test_python_2_hint() -> None:
211 with pytest.raises(black.parsing.InvalidInput) as exc_info:
212 assert_format("print 'daylily'", "print 'daylily'")
213 exc_info.match(black.parsing.PY2_HINT)
216 def test_docstring_no_string_normalization() -> None:
217 """Like test_docstring but with string normalization off."""
218 source, expected = read_data("docstring_no_string_normalization")
219 mode = replace(DEFAULT_MODE, string_normalization=False)
220 assert_format(source, expected, mode)
223 def test_long_strings_flag_disabled() -> None:
224 """Tests for turning off the string processing logic."""
225 source, expected = read_data("long_strings_flag_disabled")
226 mode = replace(DEFAULT_MODE, experimental_string_processing=False)
227 assert_format(source, expected, mode)
230 def test_numeric_literals() -> None:
231 source, expected = read_data("numeric_literals")
232 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
233 assert_format(source, expected, mode)
236 def test_numeric_literals_ignoring_underscores() -> None:
237 source, expected = read_data("numeric_literals_skip_underscores")
238 mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
239 assert_format(source, expected, mode)
242 def test_stub() -> None:
243 mode = replace(DEFAULT_MODE, is_pyi=True)
244 source, expected = read_data("stub.pyi")
245 assert_format(source, expected, mode)
248 def test_python38() -> None:
249 source, expected = read_data("python38")
250 assert_format(source, expected, minimum_version=(3, 8))
253 def test_python39() -> None:
254 source, expected = read_data("python39")
255 assert_format(source, expected, minimum_version=(3, 9))